[PATCH bpf-next] selftests/bpf: test access to ringbuf position with map pointer

Menglong Dong posted 1 patch 2 weeks ago
There is a newer version of this series
.../testing/selftests/bpf/progs/map_ptr_kern.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
[PATCH bpf-next] selftests/bpf: test access to ringbuf position with map pointer
Posted by Menglong Dong 2 weeks ago
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>
---
 .../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..c3fa57ec430b 100644
--- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c
+++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
@@ -647,21 +647,39 @@ 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 {
 	__uint(type, BPF_MAP_TYPE_RINGBUF);
+	__uint(max_entries, 1 << 10);
 } m_ringbuf SEC(".maps");
 
 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);
+	VERIFY(rb->producer_pos == 128 + 8);
+
 	return 1;
 }
 
-- 
2.53.0
Re: [PATCH bpf-next] selftests/bpf: test access to ringbuf position with map pointer
Posted by Emil Tsalapatis 1 week, 6 days ago
On Fri Mar 20, 2026 at 4:41 AM EDT, 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.
>
> Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>

Once Jiri's feedback is addressed feel free to add:

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

One nit below.

> ---
>  .../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..c3fa57ec430b 100644
> --- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> +++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> @@ -647,21 +647,39 @@ 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 {
>  	__uint(type, BPF_MAP_TYPE_RINGBUF);
> +	__uint(max_entries, 1 << 10);
>  } m_ringbuf SEC(".maps");
>  
>  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);

Nit: Can you repeat here the explanation from the cover letter 
as to why this is 128 + 8?

> +	VERIFY(rb->producer_pos == 128 + 8);
> +
>  	return 1;
>  }

>  
Re: [PATCH bpf-next] selftests/bpf: test access to ringbuf position with map pointer
Posted by Menglong Dong 1 week, 5 days ago
On 2026/3/22 09:51 Emil Tsalapatis <emil@etsalapatis.com> write:
> On Fri Mar 20, 2026 at 4:41 AM EDT, 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.
> >
> > Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> 
> Once Jiri's feedback is addressed feel free to add:
> 
> Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
> 
> One nit below.
[...]
> > +	VERIFY(rb->consumer_pos == 0);
> 
> Nit: Can you repeat here the explanation from the cover letter 
> as to why this is 128 + 8?

OK, I'll add a comment here.

Thanks!
Menglong Dong

> 
> > +	VERIFY(rb->producer_pos == 128 + 8);
> > +
> >  	return 1;
> >  }
> 
> >  
>
Re: [PATCH bpf-next] selftests/bpf: test access to ringbuf position with map pointer
Posted by Jiri Olsa 2 weeks ago
On Fri, Mar 20, 2026 at 04:41:55PM +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.
> 
> Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> ---
>  .../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..c3fa57ec430b 100644
> --- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> +++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> @@ -647,21 +647,39 @@ 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 {
>  	__uint(type, BPF_MAP_TYPE_RINGBUF);
> +	__uint(max_entries, 1 << 10);

do you need to set max_entries? there's already check below
making sure it's 4096

jirka


>  } m_ringbuf SEC(".maps");
>  
>  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);
> +	VERIFY(rb->producer_pos == 128 + 8);
> +
>  	return 1;
>  }
>  
> -- 
> 2.53.0
>
Re: [PATCH bpf-next] selftests/bpf: test access to ringbuf position with map pointer
Posted by Menglong Dong 1 week, 6 days ago
On 2026/3/20 20:40, Jiri Olsa wrote:
> On Fri, Mar 20, 2026 at 04:41:55PM +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.
> > 
> > Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> > ---
> >  .../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..c3fa57ec430b 100644
> > --- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> > +++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c
> > @@ -647,21 +647,39 @@ 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 {
> >  	__uint(type, BPF_MAP_TYPE_RINGBUF);
> > +	__uint(max_entries, 1 << 10);
> 
> do you need to set max_entries? there's already check below
> making sure it's 4096

Hi, Jiri. Thanks for the reviewing. You are right, we don't
need to set the max_entries here, as it will be set in the
test_map_ptr(). I'll remove this part in V2.

Thanks!
Menglong Dong

> 
> jirka
> 
> 
> >  } m_ringbuf SEC(".maps");
> >  
> >  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);
> > +	VERIFY(rb->producer_pos == 128 + 8);
> > +
> >  	return 1;
> >  }
> >  
> 
>