Skip to contentSkip to conversation

selftests/bpf: test access to ringbuf position with map pointer

PATCH Superseded Menglong Dong 4 participants 5 messages Mar 20 – Mar 23

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 .../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
index efaf622c28dd..c3fa57ec430b 100644
@@ -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;
 }
 
Signature · 1 line
2.53.0
#1 lore raw
Quoted 24 lines · Menglong Dong

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.

---
 .../testing/selftests/bpf/progs/map_ptr_kern.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tools/testing/selftests/bpf/progs/map_ptr_kern.c +3 -0
index efaf622c28dd..c3fa57ec430b 100644
@@ -647,21 +647,39 @@ static inline int check_devmap_hash(void)
 	return 1;
 }
 
+struct bpf_ringbuf {
+	unsigned long consumer_pos;
+	unsigned long producer_pos;

tools/testing/selftests/bpf/progs/map_ptr_kern.c static inline int check_devmap_hash(void) · line 662

Quoted 10 lines
tools/testing/selftests/bpf/progs/map_ptr_kern.c +4 -0
+} __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

Quoted 26 lines
tools/testing/selftests/bpf/progs/map_ptr_kern.c +11 -0
 } 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;
 }
 
Signature · 1 line
2.53.0
#2 lore raw
Quoted 32 lines · Jiri Olsa
Quoted 31 lines · Menglong Dong

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.

---
 .../testing/selftests/bpf/progs/map_ptr_kern.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tools/testing/selftests/bpf/progs/map_ptr_kern.c +6 -0
index efaf622c28dd..c3fa57ec430b 100644
@@ -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));
 
Quoted 6 lines

tools/testing/selftests/bpf/progs/map_ptr_kern.c static inline int check_devmap_hash(void) · line 662

Quoted 3 lines
tools/testing/selftests/bpf/progs/map_ptr_kern.c +1 -0
 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

Quoted 29 lines

jirka

Quoted 23 lines
tools/testing/selftests/bpf/progs/map_ptr_kern.c +11 -0
 } 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;
 }
 
reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Menglong Dong
Quoted 9 lines · Menglong Dong

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.

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

One nit below.

Quoted 33 lines
---
 .../testing/selftests/bpf/progs/map_ptr_kern.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tools/testing/selftests/bpf/progs/map_ptr_kern.c +9 -0
index efaf622c28dd..c3fa57ec430b 100644
@@ -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;

tools/testing/selftests/bpf/progs/map_ptr_kern.c static inline int check_devmap_hash(void) · line 680

Quoted 10 lines
tools/testing/selftests/bpf/progs/map_ptr_kern.c +7 -0
 
 	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?

Quoted 6 lines
tools/testing/selftests/bpf/progs/map_ptr_kern.c +2 -0
+	VERIFY(rb->producer_pos == 128 + 8);
+
 	return 1;
 }
 
#4 lore raw

On 2026/3/22 09:51 Emil Tsalapatis <[email protected]> write:

Quoted 16 lines · Menglong Dong
Quoted 9 lines

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.

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

One nit below.

[...]

Quoted 4 lines

tools/testing/selftests/bpf/progs/map_ptr_kern.c static inline int check_devmap_hash(void) · line 680

Quoted 1 line
tools/testing/selftests/bpf/progs/map_ptr_kern.c +1 -0
+	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

Quoted 8 lines
Quoted 7 lines
tools/testing/selftests/bpf/progs/map_ptr_kern.c +2 -0
+	VERIFY(rb->producer_pos == 128 + 8);
+
 	return 1;
 }