Re: [PATCH bpf 4/6] libbpf: ringbuf: Use compiler atomics
From: Emil Tsalapatis
Date: Tue Jun 16 2026 - 21:31:02 EST
On Sat Jun 13, 2026 at 9:48 PM EDT, Tamir Duberstein wrote:
> Consumer-side ring buffer code uses architecture-specific smp_* helpers
> for shared memory accesses.
>
> Use compiler atomics instead. They provide equivalent acquire and
> release ordering through a portable userspace interface and allow the
> next commit to use compiler fences in the wakeup protocol without mixing
> atomic interfaces.
>
> Assisted-by: Codex:gpt-5.5
> Signed-off-by: Tamir Duberstein <tamird@xxxxxxxxxx>
Reviewed-by: Emil Tsalapatis <emil@xxxxxxxxxxxxxxx>
> ---
> tools/lib/bpf/ringbuf.c | 20 +++++++++++---------
> 1 file changed, 11 insertions(+), 9 deletions(-)
>
> diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c
> index b7adce37b519..1c24a83f59d5 100644
> --- a/tools/lib/bpf/ringbuf.c
> +++ b/tools/lib/bpf/ringbuf.c
> @@ -264,13 +264,13 @@ static int64_t ringbuf_process_ring(struct ring *r, size_t n)
> if (n == 0)
> return 0;
>
> - cons_pos = smp_load_acquire(r->consumer_pos);
> + cons_pos = __atomic_load_n(r->consumer_pos, __ATOMIC_ACQUIRE);
> do {
> got_new_data = false;
> - prod_pos = smp_load_acquire(r->producer_pos);
> + prod_pos = __atomic_load_n(r->producer_pos, __ATOMIC_ACQUIRE);
> while (cons_pos != prod_pos) {
> len_ptr = r->data + (cons_pos & r->mask);
> - len = smp_load_acquire(len_ptr);
> + len = __atomic_load_n(len_ptr, __ATOMIC_ACQUIRE);
>
> /* sample not committed yet, bail out for now */
> if (len & BPF_RINGBUF_BUSY_BIT)
> @@ -284,14 +284,16 @@ static int64_t ringbuf_process_ring(struct ring *r, size_t n)
> err = r->sample_cb(r->ctx, sample, len);
> if (err < 0) {
> /* update consumer pos and bail out */
> - smp_store_release(r->consumer_pos,
> - cons_pos);
> + __atomic_store_n(r->consumer_pos,
> + cons_pos,
> + __ATOMIC_RELEASE);
> return err;
> }
> cnt++;
> }
>
> - smp_store_release(r->consumer_pos, cons_pos);
> + __atomic_store_n(r->consumer_pos, cons_pos,
> + __ATOMIC_RELEASE);
>
> if (cnt >= n)
> goto done;
> @@ -406,8 +408,8 @@ struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx)
>
> unsigned long ring__consumer_pos(const struct ring *r)
> {
> - /* Synchronizes with smp_store_release() in ringbuf_process_ring(). */
> - return smp_load_acquire(r->consumer_pos);
> + /* Synchronizes with the release store in ringbuf_process_ring(). */
> + return __atomic_load_n(r->consumer_pos, __ATOMIC_ACQUIRE);
> }
>
> unsigned long ring__producer_pos(const struct ring *r)
> @@ -415,7 +417,7 @@ unsigned long ring__producer_pos(const struct ring *r)
> /* Synchronizes with smp_store_release() in __bpf_ringbuf_reserve() in
> * the kernel.
> */
> - return smp_load_acquire(r->producer_pos);
> + return __atomic_load_n(r->producer_pos, __ATOMIC_ACQUIRE);
> }
>
> size_t ring__avail_data_size(const struct ring *r)