[PATCH] bpf: relax acquire for consumer_pos in ringbuf_process_ring()

Soham Bagchi posted 1 patch 2 months ago
tools/lib/bpf/ringbuf.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] bpf: relax acquire for consumer_pos in ringbuf_process_ring()
Posted by Soham Bagchi 2 months ago
Since r->consumer_pos is modified only by the user thread
in the given ringbuf context (and as such, it is thread-local)
it does not require a load-acquire.

Signed-off-by: Soham Bagchi <soham.bagchi@utah.edu>
---
 tools/lib/bpf/ringbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c
index 9702b70da44..7753a6570cf 100644
--- a/tools/lib/bpf/ringbuf.c
+++ b/tools/lib/bpf/ringbuf.c
@@ -241,7 +241,7 @@ static int64_t ringbuf_process_ring(struct ring *r, size_t n)
 	bool got_new_data;
 	void *sample;
 
-	cons_pos = smp_load_acquire(r->consumer_pos);
+	cons_pos = *r->consumer_pos;
 	do {
 		got_new_data = false;
 		prod_pos = smp_load_acquire(r->producer_pos);
-- 
2.34.1
Re: [PATCH] bpf: relax acquire for consumer_pos in ringbuf_process_ring()
Posted by Alexei Starovoitov 2 months ago
On Wed, Jul 30, 2025 at 11:53 AM Soham Bagchi <soham.bagchi@utah.edu> wrote:
>
> Since r->consumer_pos is modified only by the user thread
> in the given ringbuf context (and as such, it is thread-local)
> it does not require a load-acquire.
>
> Signed-off-by: Soham Bagchi <soham.bagchi@utah.edu>
> ---
>  tools/lib/bpf/ringbuf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c
> index 9702b70da44..7753a6570cf 100644
> --- a/tools/lib/bpf/ringbuf.c
> +++ b/tools/lib/bpf/ringbuf.c
> @@ -241,7 +241,7 @@ static int64_t ringbuf_process_ring(struct ring *r, size_t n)
>         bool got_new_data;
>         void *sample;
>
> -       cons_pos = smp_load_acquire(r->consumer_pos);
> +       cons_pos = *r->consumer_pos;

I don't think it's correct.
See comment in __bpf_user_ringbuf_sample_release()

--
pw-bot: cr
Re: [PATCH] bpf: relax acquire for consumer_pos in ringbuf_process_ring()
Posted by Soham Bagchi 1 month, 4 weeks ago
I have the following reasons:

1. ringbuf is an MPSC, there will only be a single
consumer thread. Therefore, only a single thread
is modifying the consumer_pos variable. The only
functions that modify consumer_pos are _peek(),
_sample_release() and _process() in libbpf.

2. The producer functions should continue to
load-acquire the consumer_pos variable since it
could be executing on a different thread. These
would pair with the store_releases mentioned in (1).
Even while the BUSY_BIT is set, conflicting functions
are producer functions, which don't modify the
consumer_pos variable anyway, they modify
producer_pos [1].

3. Since the single user consumer thread is modifying
consumer_pos, and the code lines in question are
lines where the same user consumer thread would
read consumer_pos, the thread-locality of the loads
and stores mitigate the need for user-side
load-acquires.

Refs:
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/bpf/ringbuf.c?h=v6.16#n456

________________________________________
From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Sent: Thursday, July 31, 2025 10:46 AM
To: Soham Bagchi
Cc: Alexei Starovoitov; Daniel Borkmann; Andrii Nakryiko; Martin KaFai Lau; Eduard; Song Liu; Yonghong Song; John Fastabend; KP Singh; Stanislav Fomichev; Hao Luo; Jiri Olsa; bpf; LKML; Soham Bagchi
Subject: Re: [PATCH] bpf: relax acquire for consumer_pos in ringbuf_process_ring()

On Wed, Jul 30, 2025 at 11:53 AM Soham Bagchi <soham.bagchi@utah.edu> wrote:
>
> Since r->consumer_pos is modified only by the user thread
> in the given ringbuf context (and as such, it is thread-local)
> it does not require a load-acquire.
>
> Signed-off-by: Soham Bagchi <soham.bagchi@utah.edu>
> ---
>  tools/lib/bpf/ringbuf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c
> index 9702b70da44..7753a6570cf 100644
> --- a/tools/lib/bpf/ringbuf.c
> +++ b/tools/lib/bpf/ringbuf.c
> @@ -241,7 +241,7 @@ static int64_t ringbuf_process_ring(struct ring *r, size_t n)
>         bool got_new_data;
>         void *sample;
>
> -       cons_pos = smp_load_acquire(r->consumer_pos);
> +       cons_pos = *r->consumer_pos;

I don't think it's correct.
See comment in __bpf_user_ringbuf_sample_release()

--
pw-bot: cr