* [PATCH bpf-next v2] selftests/bpf: test access to ringbuf position with map pointer
@ 2026-03-23 2:18 Menglong Dong
2026-03-23 10:59 ` Jiri Olsa
2026-03-24 2:53 ` Leon Hwang
0 siblings, 2 replies; 5+ messages in thread
From: Menglong Dong @ 2026-03-23 2:18 UTC (permalink / raw)
To: ast, olsajiri
Cc: daniel, andrii, martin.lau, eddyz87, song, yonghong.song,
john.fastabend, kpsingh, sdf, haoluo, jolsa, shuah, bpf,
linux-kselftest, linux-kernel
Add the testing to access the bpf_ringbuf with the map pointer.
"consumer_pos" and "producer_pos" is accessed in this testing. We reserve
128 bytes in the ringbuf to test the producer_pos, which should be
"128 + 8", and the "8" is BPF_RINGBUF_HDR_SZ.
It will be helpful if we want to evaluate the usage of the ringbuf in bpf
prog with the consumer and producer position.
Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
---
v2:
- don't set the max_entries for the ringbuf map
- add comment for the producer_pos
---
.../testing/selftests/bpf/progs/map_ptr_kern.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/tools/testing/selftests/bpf/progs/map_ptr_kern.c b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
index efaf622c28dd..d7611e7018ca 100644
--- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c
+++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
@@ -647,8 +647,14 @@ static inline int check_devmap_hash(void)
return 1;
}
+struct bpf_ringbuf {
+ unsigned long consumer_pos;
+ unsigned long producer_pos;
+} __attribute__((preserve_access_index));
+
struct bpf_ringbuf_map {
struct bpf_map map;
+ struct bpf_ringbuf *rb;
} __attribute__((preserve_access_index));
struct {
@@ -659,9 +665,21 @@ static inline int check_ringbuf(void)
{
struct bpf_ringbuf_map *ringbuf = (struct bpf_ringbuf_map *)&m_ringbuf;
struct bpf_map *map = (struct bpf_map *)&m_ringbuf;
+ struct bpf_ringbuf *rb;
+ void *ptr;
VERIFY(check(&ringbuf->map, map, 0, 0, page_size));
+ ptr = bpf_ringbuf_reserve(&m_ringbuf, 128, 0);
+ VERIFY(ptr);
+
+ bpf_ringbuf_discard(ptr, 0);
+ rb = ringbuf->rb;
+ VERIFY(rb);
+ VERIFY(rb->consumer_pos == 0);
+ /* The "8" here is BPF_RINGBUF_HDR_SZ */
+ VERIFY(rb->producer_pos == 128 + 8);
+
return 1;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH bpf-next v2] selftests/bpf: test access to ringbuf position with map pointer
2026-03-23 2:18 [PATCH bpf-next v2] selftests/bpf: test access to ringbuf position with map pointer Menglong Dong
@ 2026-03-23 10:59 ` Jiri Olsa
2026-03-24 9:21 ` Menglong Dong
2026-03-24 2:53 ` Leon Hwang
1 sibling, 1 reply; 5+ messages in thread
From: Jiri Olsa @ 2026-03-23 10:59 UTC (permalink / raw)
To: Menglong Dong
Cc: ast, olsajiri, daniel, andrii, martin.lau, eddyz87, song,
yonghong.song, john.fastabend, kpsingh, sdf, haoluo, shuah, bpf,
linux-kselftest, linux-kernel
On Mon, Mar 23, 2026 at 10:18:39AM +0800, Menglong Dong wrote:
> Add the testing to access the bpf_ringbuf with the map pointer.
> "consumer_pos" and "producer_pos" is accessed in this testing. We reserve
> 128 bytes in the ringbuf to test the producer_pos, which should be
> "128 + 8", and the "8" is BPF_RINGBUF_HDR_SZ.
>
> It will be helpful if we want to evaluate the usage of the ringbuf in bpf
> prog with the consumer and producer position.
lgtm, question though.. is this related to some kernel change or
some ongoing work? looks like basic operation that's already
tested indirectly by existing tests
Acked-by: Jiri Olsa <jolsa@kernel.org>
jirka
>
> Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
> ---
> v2:
> - don't set the max_entries for the ringbuf map
> - add comment for the producer_pos
> ---
> .../testing/selftests/bpf/progs/map_ptr_kern.c | 18 ++++++++++++++++++
> 1 file changed, 18 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/progs/map_ptr_kern.c b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> index efaf622c28dd..d7611e7018ca 100644
> --- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> +++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> @@ -647,8 +647,14 @@ static inline int check_devmap_hash(void)
> return 1;
> }
>
> +struct bpf_ringbuf {
> + unsigned long consumer_pos;
> + unsigned long producer_pos;
> +} __attribute__((preserve_access_index));
> +
> struct bpf_ringbuf_map {
> struct bpf_map map;
> + struct bpf_ringbuf *rb;
> } __attribute__((preserve_access_index));
>
> struct {
> @@ -659,9 +665,21 @@ static inline int check_ringbuf(void)
> {
> struct bpf_ringbuf_map *ringbuf = (struct bpf_ringbuf_map *)&m_ringbuf;
> struct bpf_map *map = (struct bpf_map *)&m_ringbuf;
> + struct bpf_ringbuf *rb;
> + void *ptr;
>
> VERIFY(check(&ringbuf->map, map, 0, 0, page_size));
>
> + ptr = bpf_ringbuf_reserve(&m_ringbuf, 128, 0);
> + VERIFY(ptr);
> +
> + bpf_ringbuf_discard(ptr, 0);
> + rb = ringbuf->rb;
> + VERIFY(rb);
> + VERIFY(rb->consumer_pos == 0);
> + /* The "8" here is BPF_RINGBUF_HDR_SZ */
> + VERIFY(rb->producer_pos == 128 + 8);
> +
> return 1;
> }
>
> --
> 2.53.0
>
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH bpf-next v2] selftests/bpf: test access to ringbuf position with map pointer
2026-03-23 10:59 ` Jiri Olsa
@ 2026-03-24 9:21 ` Menglong Dong
0 siblings, 0 replies; 5+ messages in thread
From: Menglong Dong @ 2026-03-24 9:21 UTC (permalink / raw)
To: Menglong Dong, Jiri Olsa
Cc: ast, olsajiri, daniel, andrii, martin.lau, eddyz87, song,
yonghong.song, john.fastabend, kpsingh, sdf, haoluo, shuah, bpf,
linux-kselftest, linux-kernel
On 2026/3/23 18:59 Jiri Olsa <olsajiri@gmail.com> write:
> On Mon, Mar 23, 2026 at 10:18:39AM +0800, Menglong Dong wrote:
> > Add the testing to access the bpf_ringbuf with the map pointer.
> > "consumer_pos" and "producer_pos" is accessed in this testing. We reserve
> > 128 bytes in the ringbuf to test the producer_pos, which should be
> > "128 + 8", and the "8" is BPF_RINGBUF_HDR_SZ.
> >
> > It will be helpful if we want to evaluate the usage of the ringbuf in bpf
> > prog with the consumer and producer position.
>
> lgtm, question though.. is this related to some kernel change or
> some ongoing work? looks like basic operation that's already
> tested indirectly by existing tests
I wanted to introduce a kfunc to get the usage of the ringbuf,
then we can wake up the user space lazily if the free space in
the ringbuf is big enough in some case. Then, I found that we
can do it directly with the map ptr. However, I don't see a
test case that read the pointer that come from the map ptr, which
makes me not sure if it is allowed to access the
ringbuf->rb->consumer_pos. So I did the testing, and I worked :)
Thanks!
Menglong Dong
>
> Acked-by: Jiri Olsa <jolsa@kernel.org>
>
> jirka
>
> >
> > Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> > Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
> > ---
> > v2:
> > - don't set the max_entries for the ringbuf map
> > - add comment for the producer_pos
> > ---
> > .../testing/selftests/bpf/progs/map_ptr_kern.c | 18 ++++++++++++++++++
> > 1 file changed, 18 insertions(+)
> >
> > diff --git a/tools/testing/selftests/bpf/progs/map_ptr_kern.c b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> > index efaf622c28dd..d7611e7018ca 100644
> > --- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> > +++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> > @@ -647,8 +647,14 @@ static inline int check_devmap_hash(void)
> > return 1;
> > }
> >
> > +struct bpf_ringbuf {
> > + unsigned long consumer_pos;
> > + unsigned long producer_pos;
> > +} __attribute__((preserve_access_index));
> > +
> > struct bpf_ringbuf_map {
> > struct bpf_map map;
> > + struct bpf_ringbuf *rb;
> > } __attribute__((preserve_access_index));
> >
> > struct {
> > @@ -659,9 +665,21 @@ static inline int check_ringbuf(void)
> > {
> > struct bpf_ringbuf_map *ringbuf = (struct bpf_ringbuf_map *)&m_ringbuf;
> > struct bpf_map *map = (struct bpf_map *)&m_ringbuf;
> > + struct bpf_ringbuf *rb;
> > + void *ptr;
> >
> > VERIFY(check(&ringbuf->map, map, 0, 0, page_size));
> >
> > + ptr = bpf_ringbuf_reserve(&m_ringbuf, 128, 0);
> > + VERIFY(ptr);
> > +
> > + bpf_ringbuf_discard(ptr, 0);
> > + rb = ringbuf->rb;
> > + VERIFY(rb);
> > + VERIFY(rb->consumer_pos == 0);
> > + /* The "8" here is BPF_RINGBUF_HDR_SZ */
> > + VERIFY(rb->producer_pos == 128 + 8);
> > +
> > return 1;
> > }
> >
> > --
> > 2.53.0
> >
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH bpf-next v2] selftests/bpf: test access to ringbuf position with map pointer
2026-03-23 2:18 [PATCH bpf-next v2] selftests/bpf: test access to ringbuf position with map pointer Menglong Dong
2026-03-23 10:59 ` Jiri Olsa
@ 2026-03-24 2:53 ` Leon Hwang
2026-03-24 9:24 ` Menglong Dong
1 sibling, 1 reply; 5+ messages in thread
From: Leon Hwang @ 2026-03-24 2:53 UTC (permalink / raw)
To: Menglong Dong, ast, olsajiri
Cc: daniel, andrii, martin.lau, eddyz87, song, yonghong.song,
john.fastabend, kpsingh, sdf, haoluo, jolsa, shuah, bpf,
linux-kselftest, linux-kernel
On 23/3/26 10:18, Menglong Dong wrote:
> Add the testing to access the bpf_ringbuf with the map pointer.
> "consumer_pos" and "producer_pos" is accessed in this testing. We reserve
> 128 bytes in the ringbuf to test the producer_pos, which should be
> "128 + 8", and the "8" is BPF_RINGBUF_HDR_SZ.
>
> It will be helpful if we want to evaluate the usage of the ringbuf in bpf
> prog with the consumer and producer position.
>
128 is a plain test for ringbuf, like those in
ringbuf_overwrite_mode_subtest().
The reserved size can be 0; however, a test for it is missing. It would
be better to add a test against 0-size here or there.
> Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
Acked-by: Leon Hwang <leon.hwang@linux.dev>
One nit below.
> ---
> v2:
> - don't set the max_entries for the ringbuf map
> - add comment for the producer_pos
> ---
> .../testing/selftests/bpf/progs/map_ptr_kern.c | 18 ++++++++++++++++++
> 1 file changed, 18 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/progs/map_ptr_kern.c b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> index efaf622c28dd..d7611e7018ca 100644
> --- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> +++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> @@ -647,8 +647,14 @@ static inline int check_devmap_hash(void)
> return 1;
> }
>
> +struct bpf_ringbuf {
> + unsigned long consumer_pos;
> + unsigned long producer_pos;
> +} __attribute__((preserve_access_index));
> +
> struct bpf_ringbuf_map {
> struct bpf_map map;
> + struct bpf_ringbuf *rb;
> } __attribute__((preserve_access_index));
>
> struct {
> @@ -659,9 +665,21 @@ static inline int check_ringbuf(void)
> {
> struct bpf_ringbuf_map *ringbuf = (struct bpf_ringbuf_map *)&m_ringbuf;
> struct bpf_map *map = (struct bpf_map *)&m_ringbuf;
> + struct bpf_ringbuf *rb;
> + void *ptr;
>
> VERIFY(check(&ringbuf->map, map, 0, 0, page_size));
>
> + ptr = bpf_ringbuf_reserve(&m_ringbuf, 128, 0);
> + VERIFY(ptr);
> +
> + bpf_ringbuf_discard(ptr, 0);
> + rb = ringbuf->rb;
> + VERIFY(rb);
> + VERIFY(rb->consumer_pos == 0);
> + /* The "8" here is BPF_RINGBUF_HDR_SZ */
> + VERIFY(rb->producer_pos == 128 + 8);
> +
NIT: Use BPF_RINGBUF_HDR_SZ directly. Is it missing in vmlinux.h?
Thanks,
Leon
> return 1;
> }
>
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH bpf-next v2] selftests/bpf: test access to ringbuf position with map pointer
2026-03-24 2:53 ` Leon Hwang
@ 2026-03-24 9:24 ` Menglong Dong
0 siblings, 0 replies; 5+ messages in thread
From: Menglong Dong @ 2026-03-24 9:24 UTC (permalink / raw)
To: Menglong Dong, ast, olsajiri, Leon Hwang
Cc: daniel, andrii, martin.lau, eddyz87, song, yonghong.song,
john.fastabend, kpsingh, sdf, haoluo, jolsa, shuah, bpf,
linux-kselftest, linux-kernel
On 2026/3/24 10:53 Leon Hwang <leon.hwang@linux.dev> write:
> On 23/3/26 10:18, Menglong Dong wrote:
> > Add the testing to access the bpf_ringbuf with the map pointer.
> > "consumer_pos" and "producer_pos" is accessed in this testing. We reserve
> > 128 bytes in the ringbuf to test the producer_pos, which should be
> > "128 + 8", and the "8" is BPF_RINGBUF_HDR_SZ.
> >
> > It will be helpful if we want to evaluate the usage of the ringbuf in bpf
> > prog with the consumer and producer position.
> >
>
> 128 is a plain test for ringbuf, like those in
> ringbuf_overwrite_mode_subtest().
>
> The reserved size can be 0; however, a test for it is missing. It would
> be better to add a test against 0-size here or there.
>
> > Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> > Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
>
> Acked-by: Leon Hwang <leon.hwang@linux.dev>
>
> One nit below.
>
> > ---
[...]
> > +
>
> NIT: Use BPF_RINGBUF_HDR_SZ directly. Is it missing in vmlinux.h?
Yeah, it is available in vmlinux.h, I should use it directly. I'll
respin a V3.
Thanks!
Menglong Dong
>
> Thanks,
> Leon
>
> > return 1;
> > }
> >
>
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-03-24 9:24 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-23 2:18 [PATCH bpf-next v2] selftests/bpf: test access to ringbuf position with map pointer Menglong Dong
2026-03-23 10:59 ` Jiri Olsa
2026-03-24 9:21 ` Menglong Dong
2026-03-24 2:53 ` Leon Hwang
2026-03-24 9:24 ` Menglong Dong
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox