1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <linux/compiler.h>
4 #include <asm/barrier.h>
5 #include <test_progs.h>
6 #include <sys/mman.h>
7 #include <sys/epoll.h>
8 #include <time.h>
9 #include <sched.h>
10 #include <signal.h>
11 #include <pthread.h>
12 #include <sys/sysinfo.h>
13 #include <linux/perf_event.h>
14 #include <linux/ring_buffer.h>
15 #include "test_ringbuf.lskel.h"
16 
17 #define EDONE 7777
18 
19 static int duration = 0;
20 
21 struct sample {
22 	int pid;
23 	int seq;
24 	long value;
25 	char comm[16];
26 };
27 
28 static int sample_cnt;
29 
30 static void atomic_inc(int *cnt)
31 {
32 	__atomic_add_fetch(cnt, 1, __ATOMIC_SEQ_CST);
33 }
34 
35 static int atomic_xchg(int *cnt, int val)
36 {
37 	return __atomic_exchange_n(cnt, val, __ATOMIC_SEQ_CST);
38 }
39 
40 static int process_sample(void *ctx, void *data, size_t len)
41 {
42 	struct sample *s = data;
43 
44 	atomic_inc(&sample_cnt);
45 
46 	switch (s->seq) {
47 	case 0:
48 		CHECK(s->value != 333, "sample1_value", "exp %ld, got %ld\n",
49 		      333L, s->value);
50 		return 0;
51 	case 1:
52 		CHECK(s->value != 777, "sample2_value", "exp %ld, got %ld\n",
53 		      777L, s->value);
54 		return -EDONE;
55 	default:
56 		/* we don't care about the rest */
57 		return 0;
58 	}
59 }
60 
61 static struct test_ringbuf *skel;
62 static struct ring_buffer *ringbuf;
63 
64 static void trigger_samples()
65 {
66 	skel->bss->dropped = 0;
67 	skel->bss->total = 0;
68 	skel->bss->discarded = 0;
69 
70 	/* trigger exactly two samples */
71 	skel->bss->value = 333;
72 	syscall(__NR_getpgid);
73 	skel->bss->value = 777;
74 	syscall(__NR_getpgid);
75 }
76 
77 static void *poll_thread(void *input)
78 {
79 	long timeout = (long)input;
80 
81 	return (void *)(long)ring_buffer__poll(ringbuf, timeout);
82 }
83 
84 void test_ringbuf(void)
85 {
86 	const size_t rec_sz = BPF_RINGBUF_HDR_SZ + sizeof(struct sample);
87 	pthread_t thread;
88 	long bg_ret = -1;
89 	int err, cnt;
90 	int page_size = getpagesize();
91 
92 	skel = test_ringbuf__open();
93 	if (CHECK(!skel, "skel_open", "skeleton open failed\n"))
94 		return;
95 
96 	skel->maps.ringbuf.max_entries = page_size;
97 
98 	err = test_ringbuf__load(skel);
99 	if (CHECK(err != 0, "skel_load", "skeleton load failed\n"))
100 		goto cleanup;
101 
102 	/* only trigger BPF program for current process */
103 	skel->bss->pid = getpid();
104 
105 	ringbuf = ring_buffer__new(skel->maps.ringbuf.map_fd,
106 				   process_sample, NULL, NULL);
107 	if (CHECK(!ringbuf, "ringbuf_create", "failed to create ringbuf\n"))
108 		goto cleanup;
109 
110 	err = test_ringbuf__attach(skel);
111 	if (CHECK(err, "skel_attach", "skeleton attachment failed: %d\n", err))
112 		goto cleanup;
113 
114 	trigger_samples();
115 
116 	/* 2 submitted + 1 discarded records */
117 	CHECK(skel->bss->avail_data != 3 * rec_sz,
118 	      "err_avail_size", "exp %ld, got %ld\n",
119 	      3L * rec_sz, skel->bss->avail_data);
120 	CHECK(skel->bss->ring_size != page_size,
121 	      "err_ring_size", "exp %ld, got %ld\n",
122 	      (long)page_size, skel->bss->ring_size);
123 	CHECK(skel->bss->cons_pos != 0,
124 	      "err_cons_pos", "exp %ld, got %ld\n",
125 	      0L, skel->bss->cons_pos);
126 	CHECK(skel->bss->prod_pos != 3 * rec_sz,
127 	      "err_prod_pos", "exp %ld, got %ld\n",
128 	      3L * rec_sz, skel->bss->prod_pos);
129 
130 	/* poll for samples */
131 	err = ring_buffer__poll(ringbuf, -1);
132 
133 	/* -EDONE is used as an indicator that we are done */
134 	if (CHECK(err != -EDONE, "err_done", "done err: %d\n", err))
135 		goto cleanup;
136 	cnt = atomic_xchg(&sample_cnt, 0);
137 	CHECK(cnt != 2, "cnt", "exp %d samples, got %d\n", 2, cnt);
138 
139 	/* we expect extra polling to return nothing */
140 	err = ring_buffer__poll(ringbuf, 0);
141 	if (CHECK(err != 0, "extra_samples", "poll result: %d\n", err))
142 		goto cleanup;
143 	cnt = atomic_xchg(&sample_cnt, 0);
144 	CHECK(cnt != 0, "cnt", "exp %d samples, got %d\n", 0, cnt);
145 
146 	CHECK(skel->bss->dropped != 0, "err_dropped", "exp %ld, got %ld\n",
147 	      0L, skel->bss->dropped);
148 	CHECK(skel->bss->total != 2, "err_total", "exp %ld, got %ld\n",
149 	      2L, skel->bss->total);
150 	CHECK(skel->bss->discarded != 1, "err_discarded", "exp %ld, got %ld\n",
151 	      1L, skel->bss->discarded);
152 
153 	/* now validate consumer position is updated and returned */
154 	trigger_samples();
155 	CHECK(skel->bss->cons_pos != 3 * rec_sz,
156 	      "err_cons_pos", "exp %ld, got %ld\n",
157 	      3L * rec_sz, skel->bss->cons_pos);
158 	err = ring_buffer__poll(ringbuf, -1);
159 	CHECK(err <= 0, "poll_err", "err %d\n", err);
160 	cnt = atomic_xchg(&sample_cnt, 0);
161 	CHECK(cnt != 2, "cnt", "exp %d samples, got %d\n", 2, cnt);
162 
163 	/* start poll in background w/ long timeout */
164 	err = pthread_create(&thread, NULL, poll_thread, (void *)(long)10000);
165 	if (CHECK(err, "bg_poll", "pthread_create failed: %d\n", err))
166 		goto cleanup;
167 
168 	/* turn off notifications now */
169 	skel->bss->flags = BPF_RB_NO_WAKEUP;
170 
171 	/* give background thread a bit of a time */
172 	usleep(50000);
173 	trigger_samples();
174 	/* sleeping arbitrarily is bad, but no better way to know that
175 	 * epoll_wait() **DID NOT** unblock in background thread
176 	 */
177 	usleep(50000);
178 	/* background poll should still be blocked */
179 	err = pthread_tryjoin_np(thread, (void **)&bg_ret);
180 	if (CHECK(err != EBUSY, "try_join", "err %d\n", err))
181 		goto cleanup;
182 
183 	/* BPF side did everything right */
184 	CHECK(skel->bss->dropped != 0, "err_dropped", "exp %ld, got %ld\n",
185 	      0L, skel->bss->dropped);
186 	CHECK(skel->bss->total != 2, "err_total", "exp %ld, got %ld\n",
187 	      2L, skel->bss->total);
188 	CHECK(skel->bss->discarded != 1, "err_discarded", "exp %ld, got %ld\n",
189 	      1L, skel->bss->discarded);
190 	cnt = atomic_xchg(&sample_cnt, 0);
191 	CHECK(cnt != 0, "cnt", "exp %d samples, got %d\n", 0, cnt);
192 
193 	/* clear flags to return to "adaptive" notification mode */
194 	skel->bss->flags = 0;
195 
196 	/* produce new samples, no notification should be triggered, because
197 	 * consumer is now behind
198 	 */
199 	trigger_samples();
200 
201 	/* background poll should still be blocked */
202 	err = pthread_tryjoin_np(thread, (void **)&bg_ret);
203 	if (CHECK(err != EBUSY, "try_join", "err %d\n", err))
204 		goto cleanup;
205 
206 	/* still no samples, because consumer is behind */
207 	cnt = atomic_xchg(&sample_cnt, 0);
208 	CHECK(cnt != 0, "cnt", "exp %d samples, got %d\n", 0, cnt);
209 
210 	skel->bss->dropped = 0;
211 	skel->bss->total = 0;
212 	skel->bss->discarded = 0;
213 
214 	skel->bss->value = 333;
215 	syscall(__NR_getpgid);
216 	/* now force notifications */
217 	skel->bss->flags = BPF_RB_FORCE_WAKEUP;
218 	skel->bss->value = 777;
219 	syscall(__NR_getpgid);
220 
221 	/* now we should get a pending notification */
222 	usleep(50000);
223 	err = pthread_tryjoin_np(thread, (void **)&bg_ret);
224 	if (CHECK(err, "join_bg", "err %d\n", err))
225 		goto cleanup;
226 
227 	if (CHECK(bg_ret <= 0, "bg_ret", "epoll_wait result: %ld", bg_ret))
228 		goto cleanup;
229 
230 	/* due to timing variations, there could still be non-notified
231 	 * samples, so consume them here to collect all the samples
232 	 */
233 	err = ring_buffer__consume(ringbuf);
234 	CHECK(err < 0, "rb_consume", "failed: %d\b", err);
235 
236 	/* 3 rounds, 2 samples each */
237 	cnt = atomic_xchg(&sample_cnt, 0);
238 	CHECK(cnt != 6, "cnt", "exp %d samples, got %d\n", 6, cnt);
239 
240 	/* BPF side did everything right */
241 	CHECK(skel->bss->dropped != 0, "err_dropped", "exp %ld, got %ld\n",
242 	      0L, skel->bss->dropped);
243 	CHECK(skel->bss->total != 2, "err_total", "exp %ld, got %ld\n",
244 	      2L, skel->bss->total);
245 	CHECK(skel->bss->discarded != 1, "err_discarded", "exp %ld, got %ld\n",
246 	      1L, skel->bss->discarded);
247 
248 	test_ringbuf__detach(skel);
249 cleanup:
250 	ring_buffer__free(ringbuf);
251 	test_ringbuf__destroy(skel);
252 }
253