selftests/bpf: test access to ringbuf position with map pointer
Subsystems (3)
PATCH v2 1/1 selftests/bpf: test access to ringbuf position with map pointer
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.
---
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(+)
tools/testing/selftests/bpf/progs/map_ptr_kern.c +18 -0
@@ -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 ; }
Signature · 1 line
2.53.0
Quoted 7 lines · Menglong Dong
Add the testing to access the
bpf_ringbufwith the map pointer. "consumer_pos" and "producer_pos" is accessed in this testing. We reserve 128 bytes in the ringbuf to test theproducer_pos, which should be "128 + 8", and the "8" isBPF_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
jirka
Quoted 55 lines
Signed-off-by Menglong Dong <[email protected]>Reviewed-by Emil Tsalapatis <[email protected]>---
v2:
- don't set themax_entriesfor the ringbuf map
- add comment for theproducer_pos--- .../testing/selftests/bpf/progs/map_ptr_kern.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)tools/testing/selftests/bpf/progs/map_ptr_kern.c +18
-0@@ -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 ; }Signature · 1 line
2.53.0
On 2026/3/23 18:59 Jiri Olsa <[email protected]> write:
Quoted 6 lines · Menglong Dong
Quoted 5 lines
Add the testing to access the
bpf_ringbufwith the map pointer. "consumer_pos" and "producer_pos" is accessed in this testing. We reserve 128 bytes in the ringbuf to test theproducer_pos, which should be "128 + 8", and the "8" isBPF_RINGBUF_HDR_SZ.
Quoted 6 lines
Quoted 2 lines
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
Quoted 62 lines
Acked-by Jiri Olsa <[email protected]>jirka
Quoted 55 lines
Signed-off-by Menglong Dong <[email protected]>Reviewed-by Emil Tsalapatis <[email protected]>---
v2:
- don't set themax_entriesfor the ringbuf map
- add comment for theproducer_pos--- .../testing/selftests/bpf/progs/map_ptr_kern.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)tools/testing/selftests/bpf/progs/map_ptr_kern.c +18
-0@@ -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 ; }Signature · 1 line
2.53.0
Quoted 8 lines · Menglong Dong
Add the testing to access the
bpf_ringbufwith the map pointer. "consumer_pos" and "producer_pos" is accessed in this testing. We reserve 128 bytes in the ringbuf to test theproducer_pos, which should be "128 + 8", and the "8" isBPF_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 inringbuf_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.
Quoted 2 lines
Signed-off-by Menglong Dong <[email protected]>Reviewed-by Emil Tsalapatis <[email protected]>
One nit below.
Quoted 36 lines
---
v2:
- don't set themax_entriesfor the ringbuf map
- add comment for theproducer_pos--- .../testing/selftests/bpf/progs/map_ptr_kern.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)tools/testing/selftests/bpf/progs/map_ptr_kern.c +8
-0@@ -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));
tools/testing/selftests/bpf/progs/map_ptr_kern.c static inline int check_ringbuf(void) · line 682
Quoted 10 lines
tools/testing/selftests/bpf/progs/map_ptr_kern.c +10
-0+ 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
Quoted 3 lines
tools/testing/selftests/bpf/progs/map_ptr_kern.c +0
-0return 1 ; }
On 2026/3/24 10:53 Leon Hwang <[email protected]> write:
Quoted 24 lines · Menglong Dong
Quoted 8 lines
Add the testing to access the
bpf_ringbufwith the map pointer. "consumer_pos" and "producer_pos" is accessed in this testing. We reserve 128 bytes in the ringbuf to test theproducer_pos, which should be "128 + 8", and the "8" isBPF_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.
Quoted 2 lines
Signed-off-by Menglong Dong <[email protected]>Reviewed-by Emil Tsalapatis <[email protected]>Acked-by Leon Hwang <[email protected]>One nit below.
Quoted 1 line
---
[...]
Quoted 3 lines
Quoted 1 line
+
NIT: Use
BPF_RINGBUF_HDR_SZdirectly. Is it missing invmlinux.h?
Yeah, it is available in vmlinux.h, I should use it directly. I'll respin a V3.
Thanks!
Menglong Dong
Quoted 10 lines
Thanks,
LeonQuoted 3 lines
return 1;
}