Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c05ee60

Browse files
borkmannpull[bot]
authored andcommittedJun 28, 2024
selftests/bpf: Add more ring buffer test coverage
Add test coverage for reservations beyond the ring buffer size in order to validate that bpf_ringbuf_reserve() rejects the request with NULL, all other ring buffer tests keep passing as well: # ./vmtest.sh -- ./test_progs -t ringbuf [...] ./test_progs -t ringbuf [ 1.165434] bpf_testmod: loading out-of-tree module taints kernel. [ 1.165825] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel [ 1.284001] tsc: Refined TSC clocksource calibration: 3407.982 MHz [ 1.286871] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x311fc34e357, max_idle_ns: 440795379773 ns [ 1.289555] clocksource: Switched to clocksource tsc torvalds#274/1 ringbuf/ringbuf:OK torvalds#274/2 ringbuf/ringbuf_n:OK torvalds#274/3 ringbuf/ringbuf_map_key:OK torvalds#274/4 ringbuf/ringbuf_write:OK torvalds#274 ringbuf:OK torvalds#275 ringbuf_multi:OK [...] Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> [ Test fixups for getting BPF CI back to work ] Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20240621140828.18238-2-daniel@iogearbox.net
1 parent 180b35b commit c05ee60

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed
 

‎tools/testing/selftests/bpf/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
457457
LSKELS := fentry_test.c fexit_test.c fexit_sleep.c atomics.c \
458458
trace_printk.c trace_vprintk.c map_ptr_kern.c \
459459
core_kern.c core_kern_overflow.c test_ringbuf.c \
460-
test_ringbuf_n.c test_ringbuf_map_key.c
460+
test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c
461461

462462
# Generate both light skeleton and libbpf skeleton for these
463463
LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \

‎tools/testing/selftests/bpf/prog_tests/ringbuf.c

+56
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
#include <sys/sysinfo.h>
1313
#include <linux/perf_event.h>
1414
#include <linux/ring_buffer.h>
15+
1516
#include "test_ringbuf.lskel.h"
1617
#include "test_ringbuf_n.lskel.h"
1718
#include "test_ringbuf_map_key.lskel.h"
19+
#include "test_ringbuf_write.lskel.h"
1820

1921
#define EDONE 7777
2022

@@ -84,6 +86,58 @@ static void *poll_thread(void *input)
8486
return (void *)(long)ring_buffer__poll(ringbuf, timeout);
8587
}
8688

89+
static void ringbuf_write_subtest(void)
90+
{
91+
struct test_ringbuf_write_lskel *skel;
92+
int page_size = getpagesize();
93+
size_t *mmap_ptr;
94+
int err, rb_fd;
95+
96+
skel = test_ringbuf_write_lskel__open();
97+
if (!ASSERT_OK_PTR(skel, "skel_open"))
98+
return;
99+
100+
skel->maps.ringbuf.max_entries = 0x4000;
101+
102+
err = test_ringbuf_write_lskel__load(skel);
103+
if (!ASSERT_OK(err, "skel_load"))
104+
goto cleanup;
105+
106+
rb_fd = skel->maps.ringbuf.map_fd;
107+
108+
mmap_ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, rb_fd, 0);
109+
if (!ASSERT_OK_PTR(mmap_ptr, "rw_cons_pos"))
110+
goto cleanup;
111+
*mmap_ptr = 0x3000;
112+
ASSERT_OK(munmap(mmap_ptr, page_size), "unmap_rw");
113+
114+
skel->bss->pid = getpid();
115+
116+
ringbuf = ring_buffer__new(rb_fd, process_sample, NULL, NULL);
117+
if (!ASSERT_OK_PTR(ringbuf, "ringbuf_new"))
118+
goto cleanup;
119+
120+
err = test_ringbuf_write_lskel__attach(skel);
121+
if (!ASSERT_OK(err, "skel_attach"))
122+
goto cleanup_ringbuf;
123+
124+
skel->bss->discarded = 0;
125+
skel->bss->passed = 0;
126+
127+
/* trigger exactly two samples */
128+
syscall(__NR_getpgid);
129+
syscall(__NR_getpgid);
130+
131+
ASSERT_EQ(skel->bss->discarded, 2, "discarded");
132+
ASSERT_EQ(skel->bss->passed, 0, "passed");
133+
134+
test_ringbuf_write_lskel__detach(skel);
135+
cleanup_ringbuf:
136+
ring_buffer__free(ringbuf);
137+
cleanup:
138+
test_ringbuf_write_lskel__destroy(skel);
139+
}
140+
87141
static void ringbuf_subtest(void)
88142
{
89143
const size_t rec_sz = BPF_RINGBUF_HDR_SZ + sizeof(struct sample);
@@ -451,4 +505,6 @@ void test_ringbuf(void)
451505
ringbuf_n_subtest();
452506
if (test__start_subtest("ringbuf_map_key"))
453507
ringbuf_map_key_subtest();
508+
if (test__start_subtest("ringbuf_write"))
509+
ringbuf_write_subtest();
454510
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/bpf.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
struct {
10+
__uint(type, BPF_MAP_TYPE_RINGBUF);
11+
} ringbuf SEC(".maps");
12+
13+
/* inputs */
14+
int pid = 0;
15+
16+
/* outputs */
17+
long passed = 0;
18+
long discarded = 0;
19+
20+
SEC("fentry/" SYS_PREFIX "sys_getpgid")
21+
int test_ringbuf_write(void *ctx)
22+
{
23+
int *foo, cur_pid = bpf_get_current_pid_tgid() >> 32;
24+
void *sample1, *sample2;
25+
26+
if (cur_pid != pid)
27+
return 0;
28+
29+
sample1 = bpf_ringbuf_reserve(&ringbuf, 0x3000, 0);
30+
if (!sample1)
31+
return 0;
32+
/* first one can pass */
33+
sample2 = bpf_ringbuf_reserve(&ringbuf, 0x3000, 0);
34+
if (!sample2) {
35+
bpf_ringbuf_discard(sample1, 0);
36+
__sync_fetch_and_add(&discarded, 1);
37+
return 0;
38+
}
39+
/* second one must not */
40+
__sync_fetch_and_add(&passed, 1);
41+
foo = sample2 + 4084;
42+
*foo = 256;
43+
bpf_ringbuf_discard(sample1, 0);
44+
bpf_ringbuf_discard(sample2, 0);
45+
return 0;
46+
}

0 commit comments

Comments
 (0)
Please sign in to comment.