Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: "Emil Tsalapatis" <emil@etsalapatis.com>
To: "Tamir Duberstein" <tamird@kernel.org>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"Song Liu" <song@kernel.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"Jiri Olsa" <jolsa@kernel.org>, "Shuah Khan" <shuah@kernel.org>,
	"Andrea Righi" <arighi@nvidia.com>,
	"Xu Kuohai" <xukuohai@huawei.com>,
	"Andrea Righi" <andrea.righi@canonical.com>
Cc: <bpf@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linux-kselftest@vger.kernel.org>,
	"Andrew Werner" <awerner32@gmail.com>,
	"Zvi Effron" <zeffron@riotgames.com>,
	"Andrii Nakryiko" <andriin@fb.com>
Subject: Re: [PATCH bpf 4/6] libbpf: ringbuf: Use compiler atomics
Date: Tue, 16 Jun 2026 21:30:38 -0400	[thread overview]
Message-ID: <DJAXPAPT515H.2W1O5R59RISH7@etsalapatis.com> (raw)
In-Reply-To: <20260613-bpf-ringbuf-fixes-v1-4-e623481cb724@kernel.org>

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@kernel.org>

Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>

> ---
>  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)


  reply	other threads:[~2026-06-17  1:30 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-14  1:48 [PATCH bpf 0/6] libbpf: Fix ring buffer consumption Tamir Duberstein
2026-06-14  1:48 ` [PATCH bpf 1/6] libbpf: ringbuf: Honor zero consume bounds Tamir Duberstein
2026-06-17  0:35   ` Emil Tsalapatis
2026-06-14  1:48 ` [PATCH bpf 2/6] libbpf: ringbuf: Prevent NULL callback crash Tamir Duberstein
2026-06-17  0:44   ` Emil Tsalapatis
2026-06-14  1:48 ` [PATCH bpf 3/6] libbpf: ringbuf: Handle position counter wrap Tamir Duberstein
2026-06-14  1:48 ` [PATCH bpf 4/6] libbpf: ringbuf: Use compiler atomics Tamir Duberstein
2026-06-17  1:30   ` Emil Tsalapatis [this message]
2026-06-14  1:48 ` [PATCH bpf 5/6] libbpf: ringbuf: Prevent missed wakeups Tamir Duberstein
2026-06-14  1:48 ` [PATCH bpf 6/6] libbpf: ringbuf: Reject overwrite callback use Tamir Duberstein

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=DJAXPAPT515H.2W1O5R59RISH7@etsalapatis.com \
    --to=emil@etsalapatis.com \
    --cc=andrea.righi@canonical.com \
    --cc=andrii@kernel.org \
    --cc=andriin@fb.com \
    --cc=arighi@nvidia.com \
    --cc=ast@kernel.org \
    --cc=awerner32@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=tamird@kernel.org \
    --cc=xukuohai@huawei.com \
    --cc=yonghong.song@linux.dev \
    --cc=zeffron@riotgames.com \
    /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