1*f7c14bbaSAndroid Build Coastguard Worker // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*f7c14bbaSAndroid Build Coastguard Worker /*
3*f7c14bbaSAndroid Build Coastguard Worker * Ring buffer operations.
4*f7c14bbaSAndroid Build Coastguard Worker *
5*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2020 Facebook, Inc.
6*f7c14bbaSAndroid Build Coastguard Worker */
7*f7c14bbaSAndroid Build Coastguard Worker #ifndef _GNU_SOURCE
8*f7c14bbaSAndroid Build Coastguard Worker #define _GNU_SOURCE
9*f7c14bbaSAndroid Build Coastguard Worker #endif
10*f7c14bbaSAndroid Build Coastguard Worker #include <stdlib.h>
11*f7c14bbaSAndroid Build Coastguard Worker #include <stdio.h>
12*f7c14bbaSAndroid Build Coastguard Worker #include <errno.h>
13*f7c14bbaSAndroid Build Coastguard Worker #include <unistd.h>
14*f7c14bbaSAndroid Build Coastguard Worker #include <linux/err.h>
15*f7c14bbaSAndroid Build Coastguard Worker #include <linux/bpf.h>
16*f7c14bbaSAndroid Build Coastguard Worker #include <asm/barrier.h>
17*f7c14bbaSAndroid Build Coastguard Worker #include <sys/mman.h>
18*f7c14bbaSAndroid Build Coastguard Worker #include <sys/epoll.h>
19*f7c14bbaSAndroid Build Coastguard Worker #include <time.h>
20*f7c14bbaSAndroid Build Coastguard Worker
21*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf.h"
22*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_internal.h"
23*f7c14bbaSAndroid Build Coastguard Worker #include "bpf.h"
24*f7c14bbaSAndroid Build Coastguard Worker
25*f7c14bbaSAndroid Build Coastguard Worker struct ring {
26*f7c14bbaSAndroid Build Coastguard Worker ring_buffer_sample_fn sample_cb;
27*f7c14bbaSAndroid Build Coastguard Worker void *ctx;
28*f7c14bbaSAndroid Build Coastguard Worker void *data;
29*f7c14bbaSAndroid Build Coastguard Worker unsigned long *consumer_pos;
30*f7c14bbaSAndroid Build Coastguard Worker unsigned long *producer_pos;
31*f7c14bbaSAndroid Build Coastguard Worker unsigned long mask;
32*f7c14bbaSAndroid Build Coastguard Worker int map_fd;
33*f7c14bbaSAndroid Build Coastguard Worker };
34*f7c14bbaSAndroid Build Coastguard Worker
35*f7c14bbaSAndroid Build Coastguard Worker struct ring_buffer {
36*f7c14bbaSAndroid Build Coastguard Worker struct epoll_event *events;
37*f7c14bbaSAndroid Build Coastguard Worker struct ring **rings;
38*f7c14bbaSAndroid Build Coastguard Worker size_t page_size;
39*f7c14bbaSAndroid Build Coastguard Worker int epoll_fd;
40*f7c14bbaSAndroid Build Coastguard Worker int ring_cnt;
41*f7c14bbaSAndroid Build Coastguard Worker };
42*f7c14bbaSAndroid Build Coastguard Worker
43*f7c14bbaSAndroid Build Coastguard Worker struct user_ring_buffer {
44*f7c14bbaSAndroid Build Coastguard Worker struct epoll_event event;
45*f7c14bbaSAndroid Build Coastguard Worker unsigned long *consumer_pos;
46*f7c14bbaSAndroid Build Coastguard Worker unsigned long *producer_pos;
47*f7c14bbaSAndroid Build Coastguard Worker void *data;
48*f7c14bbaSAndroid Build Coastguard Worker unsigned long mask;
49*f7c14bbaSAndroid Build Coastguard Worker size_t page_size;
50*f7c14bbaSAndroid Build Coastguard Worker int map_fd;
51*f7c14bbaSAndroid Build Coastguard Worker int epoll_fd;
52*f7c14bbaSAndroid Build Coastguard Worker };
53*f7c14bbaSAndroid Build Coastguard Worker
54*f7c14bbaSAndroid Build Coastguard Worker /* 8-byte ring buffer header structure */
55*f7c14bbaSAndroid Build Coastguard Worker struct ringbuf_hdr {
56*f7c14bbaSAndroid Build Coastguard Worker __u32 len;
57*f7c14bbaSAndroid Build Coastguard Worker __u32 pad;
58*f7c14bbaSAndroid Build Coastguard Worker };
59*f7c14bbaSAndroid Build Coastguard Worker
ringbuf_free_ring(struct ring_buffer * rb,struct ring * r)60*f7c14bbaSAndroid Build Coastguard Worker static void ringbuf_free_ring(struct ring_buffer *rb, struct ring *r)
61*f7c14bbaSAndroid Build Coastguard Worker {
62*f7c14bbaSAndroid Build Coastguard Worker if (r->consumer_pos) {
63*f7c14bbaSAndroid Build Coastguard Worker munmap(r->consumer_pos, rb->page_size);
64*f7c14bbaSAndroid Build Coastguard Worker r->consumer_pos = NULL;
65*f7c14bbaSAndroid Build Coastguard Worker }
66*f7c14bbaSAndroid Build Coastguard Worker if (r->producer_pos) {
67*f7c14bbaSAndroid Build Coastguard Worker munmap(r->producer_pos, rb->page_size + 2 * (r->mask + 1));
68*f7c14bbaSAndroid Build Coastguard Worker r->producer_pos = NULL;
69*f7c14bbaSAndroid Build Coastguard Worker }
70*f7c14bbaSAndroid Build Coastguard Worker
71*f7c14bbaSAndroid Build Coastguard Worker free(r);
72*f7c14bbaSAndroid Build Coastguard Worker }
73*f7c14bbaSAndroid Build Coastguard Worker
74*f7c14bbaSAndroid Build Coastguard Worker /* Add extra RINGBUF maps to this ring buffer manager */
ring_buffer__add(struct ring_buffer * rb,int map_fd,ring_buffer_sample_fn sample_cb,void * ctx)75*f7c14bbaSAndroid Build Coastguard Worker int ring_buffer__add(struct ring_buffer *rb, int map_fd,
76*f7c14bbaSAndroid Build Coastguard Worker ring_buffer_sample_fn sample_cb, void *ctx)
77*f7c14bbaSAndroid Build Coastguard Worker {
78*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_info info;
79*f7c14bbaSAndroid Build Coastguard Worker __u32 len = sizeof(info);
80*f7c14bbaSAndroid Build Coastguard Worker struct epoll_event *e;
81*f7c14bbaSAndroid Build Coastguard Worker struct ring *r;
82*f7c14bbaSAndroid Build Coastguard Worker __u64 mmap_sz;
83*f7c14bbaSAndroid Build Coastguard Worker void *tmp;
84*f7c14bbaSAndroid Build Coastguard Worker int err;
85*f7c14bbaSAndroid Build Coastguard Worker
86*f7c14bbaSAndroid Build Coastguard Worker memset(&info, 0, sizeof(info));
87*f7c14bbaSAndroid Build Coastguard Worker
88*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_get_info_by_fd(map_fd, &info, &len);
89*f7c14bbaSAndroid Build Coastguard Worker if (err) {
90*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
91*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ringbuf: failed to get map info for fd=%d: %d\n",
92*f7c14bbaSAndroid Build Coastguard Worker map_fd, err);
93*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
94*f7c14bbaSAndroid Build Coastguard Worker }
95*f7c14bbaSAndroid Build Coastguard Worker
96*f7c14bbaSAndroid Build Coastguard Worker if (info.type != BPF_MAP_TYPE_RINGBUF) {
97*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ringbuf: map fd=%d is not BPF_MAP_TYPE_RINGBUF\n",
98*f7c14bbaSAndroid Build Coastguard Worker map_fd);
99*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
100*f7c14bbaSAndroid Build Coastguard Worker }
101*f7c14bbaSAndroid Build Coastguard Worker
102*f7c14bbaSAndroid Build Coastguard Worker tmp = libbpf_reallocarray(rb->rings, rb->ring_cnt + 1, sizeof(*rb->rings));
103*f7c14bbaSAndroid Build Coastguard Worker if (!tmp)
104*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOMEM);
105*f7c14bbaSAndroid Build Coastguard Worker rb->rings = tmp;
106*f7c14bbaSAndroid Build Coastguard Worker
107*f7c14bbaSAndroid Build Coastguard Worker tmp = libbpf_reallocarray(rb->events, rb->ring_cnt + 1, sizeof(*rb->events));
108*f7c14bbaSAndroid Build Coastguard Worker if (!tmp)
109*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOMEM);
110*f7c14bbaSAndroid Build Coastguard Worker rb->events = tmp;
111*f7c14bbaSAndroid Build Coastguard Worker
112*f7c14bbaSAndroid Build Coastguard Worker r = calloc(1, sizeof(*r));
113*f7c14bbaSAndroid Build Coastguard Worker if (!r)
114*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOMEM);
115*f7c14bbaSAndroid Build Coastguard Worker rb->rings[rb->ring_cnt] = r;
116*f7c14bbaSAndroid Build Coastguard Worker
117*f7c14bbaSAndroid Build Coastguard Worker r->map_fd = map_fd;
118*f7c14bbaSAndroid Build Coastguard Worker r->sample_cb = sample_cb;
119*f7c14bbaSAndroid Build Coastguard Worker r->ctx = ctx;
120*f7c14bbaSAndroid Build Coastguard Worker r->mask = info.max_entries - 1;
121*f7c14bbaSAndroid Build Coastguard Worker
122*f7c14bbaSAndroid Build Coastguard Worker /* Map writable consumer page */
123*f7c14bbaSAndroid Build Coastguard Worker tmp = mmap(NULL, rb->page_size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
124*f7c14bbaSAndroid Build Coastguard Worker if (tmp == MAP_FAILED) {
125*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
126*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ringbuf: failed to mmap consumer page for map fd=%d: %d\n",
127*f7c14bbaSAndroid Build Coastguard Worker map_fd, err);
128*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
129*f7c14bbaSAndroid Build Coastguard Worker }
130*f7c14bbaSAndroid Build Coastguard Worker r->consumer_pos = tmp;
131*f7c14bbaSAndroid Build Coastguard Worker
132*f7c14bbaSAndroid Build Coastguard Worker /* Map read-only producer page and data pages. We map twice as big
133*f7c14bbaSAndroid Build Coastguard Worker * data size to allow simple reading of samples that wrap around the
134*f7c14bbaSAndroid Build Coastguard Worker * end of a ring buffer. See kernel implementation for details.
135*f7c14bbaSAndroid Build Coastguard Worker */
136*f7c14bbaSAndroid Build Coastguard Worker mmap_sz = rb->page_size + 2 * (__u64)info.max_entries;
137*f7c14bbaSAndroid Build Coastguard Worker if (mmap_sz != (__u64)(size_t)mmap_sz) {
138*f7c14bbaSAndroid Build Coastguard Worker err = -E2BIG;
139*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ringbuf: ring buffer size (%u) is too big\n", info.max_entries);
140*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
141*f7c14bbaSAndroid Build Coastguard Worker }
142*f7c14bbaSAndroid Build Coastguard Worker tmp = mmap(NULL, (size_t)mmap_sz, PROT_READ, MAP_SHARED, map_fd, rb->page_size);
143*f7c14bbaSAndroid Build Coastguard Worker if (tmp == MAP_FAILED) {
144*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
145*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ringbuf: failed to mmap data pages for map fd=%d: %d\n",
146*f7c14bbaSAndroid Build Coastguard Worker map_fd, err);
147*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
148*f7c14bbaSAndroid Build Coastguard Worker }
149*f7c14bbaSAndroid Build Coastguard Worker r->producer_pos = tmp;
150*f7c14bbaSAndroid Build Coastguard Worker r->data = tmp + rb->page_size;
151*f7c14bbaSAndroid Build Coastguard Worker
152*f7c14bbaSAndroid Build Coastguard Worker e = &rb->events[rb->ring_cnt];
153*f7c14bbaSAndroid Build Coastguard Worker memset(e, 0, sizeof(*e));
154*f7c14bbaSAndroid Build Coastguard Worker
155*f7c14bbaSAndroid Build Coastguard Worker e->events = EPOLLIN;
156*f7c14bbaSAndroid Build Coastguard Worker e->data.fd = rb->ring_cnt;
157*f7c14bbaSAndroid Build Coastguard Worker if (epoll_ctl(rb->epoll_fd, EPOLL_CTL_ADD, map_fd, e) < 0) {
158*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
159*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ringbuf: failed to epoll add map fd=%d: %d\n",
160*f7c14bbaSAndroid Build Coastguard Worker map_fd, err);
161*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
162*f7c14bbaSAndroid Build Coastguard Worker }
163*f7c14bbaSAndroid Build Coastguard Worker
164*f7c14bbaSAndroid Build Coastguard Worker rb->ring_cnt++;
165*f7c14bbaSAndroid Build Coastguard Worker return 0;
166*f7c14bbaSAndroid Build Coastguard Worker
167*f7c14bbaSAndroid Build Coastguard Worker err_out:
168*f7c14bbaSAndroid Build Coastguard Worker ringbuf_free_ring(rb, r);
169*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
170*f7c14bbaSAndroid Build Coastguard Worker }
171*f7c14bbaSAndroid Build Coastguard Worker
ring_buffer__free(struct ring_buffer * rb)172*f7c14bbaSAndroid Build Coastguard Worker void ring_buffer__free(struct ring_buffer *rb)
173*f7c14bbaSAndroid Build Coastguard Worker {
174*f7c14bbaSAndroid Build Coastguard Worker int i;
175*f7c14bbaSAndroid Build Coastguard Worker
176*f7c14bbaSAndroid Build Coastguard Worker if (!rb)
177*f7c14bbaSAndroid Build Coastguard Worker return;
178*f7c14bbaSAndroid Build Coastguard Worker
179*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < rb->ring_cnt; ++i)
180*f7c14bbaSAndroid Build Coastguard Worker ringbuf_free_ring(rb, rb->rings[i]);
181*f7c14bbaSAndroid Build Coastguard Worker if (rb->epoll_fd >= 0)
182*f7c14bbaSAndroid Build Coastguard Worker close(rb->epoll_fd);
183*f7c14bbaSAndroid Build Coastguard Worker
184*f7c14bbaSAndroid Build Coastguard Worker free(rb->events);
185*f7c14bbaSAndroid Build Coastguard Worker free(rb->rings);
186*f7c14bbaSAndroid Build Coastguard Worker free(rb);
187*f7c14bbaSAndroid Build Coastguard Worker }
188*f7c14bbaSAndroid Build Coastguard Worker
189*f7c14bbaSAndroid Build Coastguard Worker struct ring_buffer *
ring_buffer__new(int map_fd,ring_buffer_sample_fn sample_cb,void * ctx,const struct ring_buffer_opts * opts)190*f7c14bbaSAndroid Build Coastguard Worker ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
191*f7c14bbaSAndroid Build Coastguard Worker const struct ring_buffer_opts *opts)
192*f7c14bbaSAndroid Build Coastguard Worker {
193*f7c14bbaSAndroid Build Coastguard Worker struct ring_buffer *rb;
194*f7c14bbaSAndroid Build Coastguard Worker int err;
195*f7c14bbaSAndroid Build Coastguard Worker
196*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, ring_buffer_opts))
197*f7c14bbaSAndroid Build Coastguard Worker return errno = EINVAL, NULL;
198*f7c14bbaSAndroid Build Coastguard Worker
199*f7c14bbaSAndroid Build Coastguard Worker rb = calloc(1, sizeof(*rb));
200*f7c14bbaSAndroid Build Coastguard Worker if (!rb)
201*f7c14bbaSAndroid Build Coastguard Worker return errno = ENOMEM, NULL;
202*f7c14bbaSAndroid Build Coastguard Worker
203*f7c14bbaSAndroid Build Coastguard Worker rb->page_size = getpagesize();
204*f7c14bbaSAndroid Build Coastguard Worker
205*f7c14bbaSAndroid Build Coastguard Worker rb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
206*f7c14bbaSAndroid Build Coastguard Worker if (rb->epoll_fd < 0) {
207*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
208*f7c14bbaSAndroid Build Coastguard Worker pr_warn("ringbuf: failed to create epoll instance: %d\n", err);
209*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
210*f7c14bbaSAndroid Build Coastguard Worker }
211*f7c14bbaSAndroid Build Coastguard Worker
212*f7c14bbaSAndroid Build Coastguard Worker err = ring_buffer__add(rb, map_fd, sample_cb, ctx);
213*f7c14bbaSAndroid Build Coastguard Worker if (err)
214*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
215*f7c14bbaSAndroid Build Coastguard Worker
216*f7c14bbaSAndroid Build Coastguard Worker return rb;
217*f7c14bbaSAndroid Build Coastguard Worker
218*f7c14bbaSAndroid Build Coastguard Worker err_out:
219*f7c14bbaSAndroid Build Coastguard Worker ring_buffer__free(rb);
220*f7c14bbaSAndroid Build Coastguard Worker return errno = -err, NULL;
221*f7c14bbaSAndroid Build Coastguard Worker }
222*f7c14bbaSAndroid Build Coastguard Worker
roundup_len(__u32 len)223*f7c14bbaSAndroid Build Coastguard Worker static inline int roundup_len(__u32 len)
224*f7c14bbaSAndroid Build Coastguard Worker {
225*f7c14bbaSAndroid Build Coastguard Worker /* clear out top 2 bits (discard and busy, if set) */
226*f7c14bbaSAndroid Build Coastguard Worker len <<= 2;
227*f7c14bbaSAndroid Build Coastguard Worker len >>= 2;
228*f7c14bbaSAndroid Build Coastguard Worker /* add length prefix */
229*f7c14bbaSAndroid Build Coastguard Worker len += BPF_RINGBUF_HDR_SZ;
230*f7c14bbaSAndroid Build Coastguard Worker /* round up to 8 byte alignment */
231*f7c14bbaSAndroid Build Coastguard Worker return (len + 7) / 8 * 8;
232*f7c14bbaSAndroid Build Coastguard Worker }
233*f7c14bbaSAndroid Build Coastguard Worker
ringbuf_process_ring(struct ring * r)234*f7c14bbaSAndroid Build Coastguard Worker static int64_t ringbuf_process_ring(struct ring *r)
235*f7c14bbaSAndroid Build Coastguard Worker {
236*f7c14bbaSAndroid Build Coastguard Worker int *len_ptr, len, err;
237*f7c14bbaSAndroid Build Coastguard Worker /* 64-bit to avoid overflow in case of extreme application behavior */
238*f7c14bbaSAndroid Build Coastguard Worker int64_t cnt = 0;
239*f7c14bbaSAndroid Build Coastguard Worker unsigned long cons_pos, prod_pos;
240*f7c14bbaSAndroid Build Coastguard Worker bool got_new_data;
241*f7c14bbaSAndroid Build Coastguard Worker void *sample;
242*f7c14bbaSAndroid Build Coastguard Worker
243*f7c14bbaSAndroid Build Coastguard Worker cons_pos = smp_load_acquire(r->consumer_pos);
244*f7c14bbaSAndroid Build Coastguard Worker do {
245*f7c14bbaSAndroid Build Coastguard Worker got_new_data = false;
246*f7c14bbaSAndroid Build Coastguard Worker prod_pos = smp_load_acquire(r->producer_pos);
247*f7c14bbaSAndroid Build Coastguard Worker while (cons_pos < prod_pos) {
248*f7c14bbaSAndroid Build Coastguard Worker len_ptr = r->data + (cons_pos & r->mask);
249*f7c14bbaSAndroid Build Coastguard Worker len = smp_load_acquire(len_ptr);
250*f7c14bbaSAndroid Build Coastguard Worker
251*f7c14bbaSAndroid Build Coastguard Worker /* sample not committed yet, bail out for now */
252*f7c14bbaSAndroid Build Coastguard Worker if (len & BPF_RINGBUF_BUSY_BIT)
253*f7c14bbaSAndroid Build Coastguard Worker goto done;
254*f7c14bbaSAndroid Build Coastguard Worker
255*f7c14bbaSAndroid Build Coastguard Worker got_new_data = true;
256*f7c14bbaSAndroid Build Coastguard Worker cons_pos += roundup_len(len);
257*f7c14bbaSAndroid Build Coastguard Worker
258*f7c14bbaSAndroid Build Coastguard Worker if ((len & BPF_RINGBUF_DISCARD_BIT) == 0) {
259*f7c14bbaSAndroid Build Coastguard Worker sample = (void *)len_ptr + BPF_RINGBUF_HDR_SZ;
260*f7c14bbaSAndroid Build Coastguard Worker err = r->sample_cb(r->ctx, sample, len);
261*f7c14bbaSAndroid Build Coastguard Worker if (err < 0) {
262*f7c14bbaSAndroid Build Coastguard Worker /* update consumer pos and bail out */
263*f7c14bbaSAndroid Build Coastguard Worker smp_store_release(r->consumer_pos,
264*f7c14bbaSAndroid Build Coastguard Worker cons_pos);
265*f7c14bbaSAndroid Build Coastguard Worker return err;
266*f7c14bbaSAndroid Build Coastguard Worker }
267*f7c14bbaSAndroid Build Coastguard Worker cnt++;
268*f7c14bbaSAndroid Build Coastguard Worker }
269*f7c14bbaSAndroid Build Coastguard Worker
270*f7c14bbaSAndroid Build Coastguard Worker smp_store_release(r->consumer_pos, cons_pos);
271*f7c14bbaSAndroid Build Coastguard Worker }
272*f7c14bbaSAndroid Build Coastguard Worker } while (got_new_data);
273*f7c14bbaSAndroid Build Coastguard Worker done:
274*f7c14bbaSAndroid Build Coastguard Worker return cnt;
275*f7c14bbaSAndroid Build Coastguard Worker }
276*f7c14bbaSAndroid Build Coastguard Worker
277*f7c14bbaSAndroid Build Coastguard Worker /* Consume available ring buffer(s) data without event polling.
278*f7c14bbaSAndroid Build Coastguard Worker * Returns number of records consumed across all registered ring buffers (or
279*f7c14bbaSAndroid Build Coastguard Worker * INT_MAX, whichever is less), or negative number if any of the callbacks
280*f7c14bbaSAndroid Build Coastguard Worker * return error.
281*f7c14bbaSAndroid Build Coastguard Worker */
ring_buffer__consume(struct ring_buffer * rb)282*f7c14bbaSAndroid Build Coastguard Worker int ring_buffer__consume(struct ring_buffer *rb)
283*f7c14bbaSAndroid Build Coastguard Worker {
284*f7c14bbaSAndroid Build Coastguard Worker int64_t err, res = 0;
285*f7c14bbaSAndroid Build Coastguard Worker int i;
286*f7c14bbaSAndroid Build Coastguard Worker
287*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < rb->ring_cnt; i++) {
288*f7c14bbaSAndroid Build Coastguard Worker struct ring *ring = rb->rings[i];
289*f7c14bbaSAndroid Build Coastguard Worker
290*f7c14bbaSAndroid Build Coastguard Worker err = ringbuf_process_ring(ring);
291*f7c14bbaSAndroid Build Coastguard Worker if (err < 0)
292*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
293*f7c14bbaSAndroid Build Coastguard Worker res += err;
294*f7c14bbaSAndroid Build Coastguard Worker }
295*f7c14bbaSAndroid Build Coastguard Worker if (res > INT_MAX)
296*f7c14bbaSAndroid Build Coastguard Worker return INT_MAX;
297*f7c14bbaSAndroid Build Coastguard Worker return res;
298*f7c14bbaSAndroid Build Coastguard Worker }
299*f7c14bbaSAndroid Build Coastguard Worker
300*f7c14bbaSAndroid Build Coastguard Worker /* Poll for available data and consume records, if any are available.
301*f7c14bbaSAndroid Build Coastguard Worker * Returns number of records consumed (or INT_MAX, whichever is less), or
302*f7c14bbaSAndroid Build Coastguard Worker * negative number, if any of the registered callbacks returned error.
303*f7c14bbaSAndroid Build Coastguard Worker */
ring_buffer__poll(struct ring_buffer * rb,int timeout_ms)304*f7c14bbaSAndroid Build Coastguard Worker int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms)
305*f7c14bbaSAndroid Build Coastguard Worker {
306*f7c14bbaSAndroid Build Coastguard Worker int i, cnt;
307*f7c14bbaSAndroid Build Coastguard Worker int64_t err, res = 0;
308*f7c14bbaSAndroid Build Coastguard Worker
309*f7c14bbaSAndroid Build Coastguard Worker cnt = epoll_wait(rb->epoll_fd, rb->events, rb->ring_cnt, timeout_ms);
310*f7c14bbaSAndroid Build Coastguard Worker if (cnt < 0)
311*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-errno);
312*f7c14bbaSAndroid Build Coastguard Worker
313*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < cnt; i++) {
314*f7c14bbaSAndroid Build Coastguard Worker __u32 ring_id = rb->events[i].data.fd;
315*f7c14bbaSAndroid Build Coastguard Worker struct ring *ring = rb->rings[ring_id];
316*f7c14bbaSAndroid Build Coastguard Worker
317*f7c14bbaSAndroid Build Coastguard Worker err = ringbuf_process_ring(ring);
318*f7c14bbaSAndroid Build Coastguard Worker if (err < 0)
319*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
320*f7c14bbaSAndroid Build Coastguard Worker res += err;
321*f7c14bbaSAndroid Build Coastguard Worker }
322*f7c14bbaSAndroid Build Coastguard Worker if (res > INT_MAX)
323*f7c14bbaSAndroid Build Coastguard Worker return INT_MAX;
324*f7c14bbaSAndroid Build Coastguard Worker return res;
325*f7c14bbaSAndroid Build Coastguard Worker }
326*f7c14bbaSAndroid Build Coastguard Worker
327*f7c14bbaSAndroid Build Coastguard Worker /* Get an fd that can be used to sleep until data is available in the ring(s) */
ring_buffer__epoll_fd(const struct ring_buffer * rb)328*f7c14bbaSAndroid Build Coastguard Worker int ring_buffer__epoll_fd(const struct ring_buffer *rb)
329*f7c14bbaSAndroid Build Coastguard Worker {
330*f7c14bbaSAndroid Build Coastguard Worker return rb->epoll_fd;
331*f7c14bbaSAndroid Build Coastguard Worker }
332*f7c14bbaSAndroid Build Coastguard Worker
ring_buffer__ring(struct ring_buffer * rb,unsigned int idx)333*f7c14bbaSAndroid Build Coastguard Worker struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx)
334*f7c14bbaSAndroid Build Coastguard Worker {
335*f7c14bbaSAndroid Build Coastguard Worker if (idx >= rb->ring_cnt)
336*f7c14bbaSAndroid Build Coastguard Worker return errno = ERANGE, NULL;
337*f7c14bbaSAndroid Build Coastguard Worker
338*f7c14bbaSAndroid Build Coastguard Worker return rb->rings[idx];
339*f7c14bbaSAndroid Build Coastguard Worker }
340*f7c14bbaSAndroid Build Coastguard Worker
ring__consumer_pos(const struct ring * r)341*f7c14bbaSAndroid Build Coastguard Worker unsigned long ring__consumer_pos(const struct ring *r)
342*f7c14bbaSAndroid Build Coastguard Worker {
343*f7c14bbaSAndroid Build Coastguard Worker /* Synchronizes with smp_store_release() in ringbuf_process_ring(). */
344*f7c14bbaSAndroid Build Coastguard Worker return smp_load_acquire(r->consumer_pos);
345*f7c14bbaSAndroid Build Coastguard Worker }
346*f7c14bbaSAndroid Build Coastguard Worker
ring__producer_pos(const struct ring * r)347*f7c14bbaSAndroid Build Coastguard Worker unsigned long ring__producer_pos(const struct ring *r)
348*f7c14bbaSAndroid Build Coastguard Worker {
349*f7c14bbaSAndroid Build Coastguard Worker /* Synchronizes with smp_store_release() in __bpf_ringbuf_reserve() in
350*f7c14bbaSAndroid Build Coastguard Worker * the kernel.
351*f7c14bbaSAndroid Build Coastguard Worker */
352*f7c14bbaSAndroid Build Coastguard Worker return smp_load_acquire(r->producer_pos);
353*f7c14bbaSAndroid Build Coastguard Worker }
354*f7c14bbaSAndroid Build Coastguard Worker
ring__avail_data_size(const struct ring * r)355*f7c14bbaSAndroid Build Coastguard Worker size_t ring__avail_data_size(const struct ring *r)
356*f7c14bbaSAndroid Build Coastguard Worker {
357*f7c14bbaSAndroid Build Coastguard Worker unsigned long cons_pos, prod_pos;
358*f7c14bbaSAndroid Build Coastguard Worker
359*f7c14bbaSAndroid Build Coastguard Worker cons_pos = ring__consumer_pos(r);
360*f7c14bbaSAndroid Build Coastguard Worker prod_pos = ring__producer_pos(r);
361*f7c14bbaSAndroid Build Coastguard Worker return prod_pos - cons_pos;
362*f7c14bbaSAndroid Build Coastguard Worker }
363*f7c14bbaSAndroid Build Coastguard Worker
ring__size(const struct ring * r)364*f7c14bbaSAndroid Build Coastguard Worker size_t ring__size(const struct ring *r)
365*f7c14bbaSAndroid Build Coastguard Worker {
366*f7c14bbaSAndroid Build Coastguard Worker return r->mask + 1;
367*f7c14bbaSAndroid Build Coastguard Worker }
368*f7c14bbaSAndroid Build Coastguard Worker
ring__map_fd(const struct ring * r)369*f7c14bbaSAndroid Build Coastguard Worker int ring__map_fd(const struct ring *r)
370*f7c14bbaSAndroid Build Coastguard Worker {
371*f7c14bbaSAndroid Build Coastguard Worker return r->map_fd;
372*f7c14bbaSAndroid Build Coastguard Worker }
373*f7c14bbaSAndroid Build Coastguard Worker
ring__consume(struct ring * r)374*f7c14bbaSAndroid Build Coastguard Worker int ring__consume(struct ring *r)
375*f7c14bbaSAndroid Build Coastguard Worker {
376*f7c14bbaSAndroid Build Coastguard Worker int64_t res;
377*f7c14bbaSAndroid Build Coastguard Worker
378*f7c14bbaSAndroid Build Coastguard Worker res = ringbuf_process_ring(r);
379*f7c14bbaSAndroid Build Coastguard Worker if (res < 0)
380*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(res);
381*f7c14bbaSAndroid Build Coastguard Worker
382*f7c14bbaSAndroid Build Coastguard Worker return res > INT_MAX ? INT_MAX : res;
383*f7c14bbaSAndroid Build Coastguard Worker }
384*f7c14bbaSAndroid Build Coastguard Worker
user_ringbuf_unmap_ring(struct user_ring_buffer * rb)385*f7c14bbaSAndroid Build Coastguard Worker static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
386*f7c14bbaSAndroid Build Coastguard Worker {
387*f7c14bbaSAndroid Build Coastguard Worker if (rb->consumer_pos) {
388*f7c14bbaSAndroid Build Coastguard Worker munmap(rb->consumer_pos, rb->page_size);
389*f7c14bbaSAndroid Build Coastguard Worker rb->consumer_pos = NULL;
390*f7c14bbaSAndroid Build Coastguard Worker }
391*f7c14bbaSAndroid Build Coastguard Worker if (rb->producer_pos) {
392*f7c14bbaSAndroid Build Coastguard Worker munmap(rb->producer_pos, rb->page_size + 2 * (rb->mask + 1));
393*f7c14bbaSAndroid Build Coastguard Worker rb->producer_pos = NULL;
394*f7c14bbaSAndroid Build Coastguard Worker }
395*f7c14bbaSAndroid Build Coastguard Worker }
396*f7c14bbaSAndroid Build Coastguard Worker
user_ring_buffer__free(struct user_ring_buffer * rb)397*f7c14bbaSAndroid Build Coastguard Worker void user_ring_buffer__free(struct user_ring_buffer *rb)
398*f7c14bbaSAndroid Build Coastguard Worker {
399*f7c14bbaSAndroid Build Coastguard Worker if (!rb)
400*f7c14bbaSAndroid Build Coastguard Worker return;
401*f7c14bbaSAndroid Build Coastguard Worker
402*f7c14bbaSAndroid Build Coastguard Worker user_ringbuf_unmap_ring(rb);
403*f7c14bbaSAndroid Build Coastguard Worker
404*f7c14bbaSAndroid Build Coastguard Worker if (rb->epoll_fd >= 0)
405*f7c14bbaSAndroid Build Coastguard Worker close(rb->epoll_fd);
406*f7c14bbaSAndroid Build Coastguard Worker
407*f7c14bbaSAndroid Build Coastguard Worker free(rb);
408*f7c14bbaSAndroid Build Coastguard Worker }
409*f7c14bbaSAndroid Build Coastguard Worker
user_ringbuf_map(struct user_ring_buffer * rb,int map_fd)410*f7c14bbaSAndroid Build Coastguard Worker static int user_ringbuf_map(struct user_ring_buffer *rb, int map_fd)
411*f7c14bbaSAndroid Build Coastguard Worker {
412*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_info info;
413*f7c14bbaSAndroid Build Coastguard Worker __u32 len = sizeof(info);
414*f7c14bbaSAndroid Build Coastguard Worker __u64 mmap_sz;
415*f7c14bbaSAndroid Build Coastguard Worker void *tmp;
416*f7c14bbaSAndroid Build Coastguard Worker struct epoll_event *rb_epoll;
417*f7c14bbaSAndroid Build Coastguard Worker int err;
418*f7c14bbaSAndroid Build Coastguard Worker
419*f7c14bbaSAndroid Build Coastguard Worker memset(&info, 0, sizeof(info));
420*f7c14bbaSAndroid Build Coastguard Worker
421*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_get_info_by_fd(map_fd, &info, &len);
422*f7c14bbaSAndroid Build Coastguard Worker if (err) {
423*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
424*f7c14bbaSAndroid Build Coastguard Worker pr_warn("user ringbuf: failed to get map info for fd=%d: %d\n", map_fd, err);
425*f7c14bbaSAndroid Build Coastguard Worker return err;
426*f7c14bbaSAndroid Build Coastguard Worker }
427*f7c14bbaSAndroid Build Coastguard Worker
428*f7c14bbaSAndroid Build Coastguard Worker if (info.type != BPF_MAP_TYPE_USER_RINGBUF) {
429*f7c14bbaSAndroid Build Coastguard Worker pr_warn("user ringbuf: map fd=%d is not BPF_MAP_TYPE_USER_RINGBUF\n", map_fd);
430*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
431*f7c14bbaSAndroid Build Coastguard Worker }
432*f7c14bbaSAndroid Build Coastguard Worker
433*f7c14bbaSAndroid Build Coastguard Worker rb->map_fd = map_fd;
434*f7c14bbaSAndroid Build Coastguard Worker rb->mask = info.max_entries - 1;
435*f7c14bbaSAndroid Build Coastguard Worker
436*f7c14bbaSAndroid Build Coastguard Worker /* Map read-only consumer page */
437*f7c14bbaSAndroid Build Coastguard Worker tmp = mmap(NULL, rb->page_size, PROT_READ, MAP_SHARED, map_fd, 0);
438*f7c14bbaSAndroid Build Coastguard Worker if (tmp == MAP_FAILED) {
439*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
440*f7c14bbaSAndroid Build Coastguard Worker pr_warn("user ringbuf: failed to mmap consumer page for map fd=%d: %d\n",
441*f7c14bbaSAndroid Build Coastguard Worker map_fd, err);
442*f7c14bbaSAndroid Build Coastguard Worker return err;
443*f7c14bbaSAndroid Build Coastguard Worker }
444*f7c14bbaSAndroid Build Coastguard Worker rb->consumer_pos = tmp;
445*f7c14bbaSAndroid Build Coastguard Worker
446*f7c14bbaSAndroid Build Coastguard Worker /* Map read-write the producer page and data pages. We map the data
447*f7c14bbaSAndroid Build Coastguard Worker * region as twice the total size of the ring buffer to allow the
448*f7c14bbaSAndroid Build Coastguard Worker * simple reading and writing of samples that wrap around the end of
449*f7c14bbaSAndroid Build Coastguard Worker * the buffer. See the kernel implementation for details.
450*f7c14bbaSAndroid Build Coastguard Worker */
451*f7c14bbaSAndroid Build Coastguard Worker mmap_sz = rb->page_size + 2 * (__u64)info.max_entries;
452*f7c14bbaSAndroid Build Coastguard Worker if (mmap_sz != (__u64)(size_t)mmap_sz) {
453*f7c14bbaSAndroid Build Coastguard Worker pr_warn("user ringbuf: ring buf size (%u) is too big\n", info.max_entries);
454*f7c14bbaSAndroid Build Coastguard Worker return -E2BIG;
455*f7c14bbaSAndroid Build Coastguard Worker }
456*f7c14bbaSAndroid Build Coastguard Worker tmp = mmap(NULL, (size_t)mmap_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
457*f7c14bbaSAndroid Build Coastguard Worker map_fd, rb->page_size);
458*f7c14bbaSAndroid Build Coastguard Worker if (tmp == MAP_FAILED) {
459*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
460*f7c14bbaSAndroid Build Coastguard Worker pr_warn("user ringbuf: failed to mmap data pages for map fd=%d: %d\n",
461*f7c14bbaSAndroid Build Coastguard Worker map_fd, err);
462*f7c14bbaSAndroid Build Coastguard Worker return err;
463*f7c14bbaSAndroid Build Coastguard Worker }
464*f7c14bbaSAndroid Build Coastguard Worker
465*f7c14bbaSAndroid Build Coastguard Worker rb->producer_pos = tmp;
466*f7c14bbaSAndroid Build Coastguard Worker rb->data = tmp + rb->page_size;
467*f7c14bbaSAndroid Build Coastguard Worker
468*f7c14bbaSAndroid Build Coastguard Worker rb_epoll = &rb->event;
469*f7c14bbaSAndroid Build Coastguard Worker rb_epoll->events = EPOLLOUT;
470*f7c14bbaSAndroid Build Coastguard Worker if (epoll_ctl(rb->epoll_fd, EPOLL_CTL_ADD, map_fd, rb_epoll) < 0) {
471*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
472*f7c14bbaSAndroid Build Coastguard Worker pr_warn("user ringbuf: failed to epoll add map fd=%d: %d\n", map_fd, err);
473*f7c14bbaSAndroid Build Coastguard Worker return err;
474*f7c14bbaSAndroid Build Coastguard Worker }
475*f7c14bbaSAndroid Build Coastguard Worker
476*f7c14bbaSAndroid Build Coastguard Worker return 0;
477*f7c14bbaSAndroid Build Coastguard Worker }
478*f7c14bbaSAndroid Build Coastguard Worker
479*f7c14bbaSAndroid Build Coastguard Worker struct user_ring_buffer *
user_ring_buffer__new(int map_fd,const struct user_ring_buffer_opts * opts)480*f7c14bbaSAndroid Build Coastguard Worker user_ring_buffer__new(int map_fd, const struct user_ring_buffer_opts *opts)
481*f7c14bbaSAndroid Build Coastguard Worker {
482*f7c14bbaSAndroid Build Coastguard Worker struct user_ring_buffer *rb;
483*f7c14bbaSAndroid Build Coastguard Worker int err;
484*f7c14bbaSAndroid Build Coastguard Worker
485*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, user_ring_buffer_opts))
486*f7c14bbaSAndroid Build Coastguard Worker return errno = EINVAL, NULL;
487*f7c14bbaSAndroid Build Coastguard Worker
488*f7c14bbaSAndroid Build Coastguard Worker rb = calloc(1, sizeof(*rb));
489*f7c14bbaSAndroid Build Coastguard Worker if (!rb)
490*f7c14bbaSAndroid Build Coastguard Worker return errno = ENOMEM, NULL;
491*f7c14bbaSAndroid Build Coastguard Worker
492*f7c14bbaSAndroid Build Coastguard Worker rb->page_size = getpagesize();
493*f7c14bbaSAndroid Build Coastguard Worker
494*f7c14bbaSAndroid Build Coastguard Worker rb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
495*f7c14bbaSAndroid Build Coastguard Worker if (rb->epoll_fd < 0) {
496*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
497*f7c14bbaSAndroid Build Coastguard Worker pr_warn("user ringbuf: failed to create epoll instance: %d\n", err);
498*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
499*f7c14bbaSAndroid Build Coastguard Worker }
500*f7c14bbaSAndroid Build Coastguard Worker
501*f7c14bbaSAndroid Build Coastguard Worker err = user_ringbuf_map(rb, map_fd);
502*f7c14bbaSAndroid Build Coastguard Worker if (err)
503*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
504*f7c14bbaSAndroid Build Coastguard Worker
505*f7c14bbaSAndroid Build Coastguard Worker return rb;
506*f7c14bbaSAndroid Build Coastguard Worker
507*f7c14bbaSAndroid Build Coastguard Worker err_out:
508*f7c14bbaSAndroid Build Coastguard Worker user_ring_buffer__free(rb);
509*f7c14bbaSAndroid Build Coastguard Worker return errno = -err, NULL;
510*f7c14bbaSAndroid Build Coastguard Worker }
511*f7c14bbaSAndroid Build Coastguard Worker
user_ringbuf_commit(struct user_ring_buffer * rb,void * sample,bool discard)512*f7c14bbaSAndroid Build Coastguard Worker static void user_ringbuf_commit(struct user_ring_buffer *rb, void *sample, bool discard)
513*f7c14bbaSAndroid Build Coastguard Worker {
514*f7c14bbaSAndroid Build Coastguard Worker __u32 new_len;
515*f7c14bbaSAndroid Build Coastguard Worker struct ringbuf_hdr *hdr;
516*f7c14bbaSAndroid Build Coastguard Worker uintptr_t hdr_offset;
517*f7c14bbaSAndroid Build Coastguard Worker
518*f7c14bbaSAndroid Build Coastguard Worker hdr_offset = rb->mask + 1 + (sample - rb->data) - BPF_RINGBUF_HDR_SZ;
519*f7c14bbaSAndroid Build Coastguard Worker hdr = rb->data + (hdr_offset & rb->mask);
520*f7c14bbaSAndroid Build Coastguard Worker
521*f7c14bbaSAndroid Build Coastguard Worker new_len = hdr->len & ~BPF_RINGBUF_BUSY_BIT;
522*f7c14bbaSAndroid Build Coastguard Worker if (discard)
523*f7c14bbaSAndroid Build Coastguard Worker new_len |= BPF_RINGBUF_DISCARD_BIT;
524*f7c14bbaSAndroid Build Coastguard Worker
525*f7c14bbaSAndroid Build Coastguard Worker /* Synchronizes with smp_load_acquire() in __bpf_user_ringbuf_peek() in
526*f7c14bbaSAndroid Build Coastguard Worker * the kernel.
527*f7c14bbaSAndroid Build Coastguard Worker */
528*f7c14bbaSAndroid Build Coastguard Worker __atomic_exchange_n(&hdr->len, new_len, __ATOMIC_ACQ_REL);
529*f7c14bbaSAndroid Build Coastguard Worker }
530*f7c14bbaSAndroid Build Coastguard Worker
user_ring_buffer__discard(struct user_ring_buffer * rb,void * sample)531*f7c14bbaSAndroid Build Coastguard Worker void user_ring_buffer__discard(struct user_ring_buffer *rb, void *sample)
532*f7c14bbaSAndroid Build Coastguard Worker {
533*f7c14bbaSAndroid Build Coastguard Worker user_ringbuf_commit(rb, sample, true);
534*f7c14bbaSAndroid Build Coastguard Worker }
535*f7c14bbaSAndroid Build Coastguard Worker
user_ring_buffer__submit(struct user_ring_buffer * rb,void * sample)536*f7c14bbaSAndroid Build Coastguard Worker void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample)
537*f7c14bbaSAndroid Build Coastguard Worker {
538*f7c14bbaSAndroid Build Coastguard Worker user_ringbuf_commit(rb, sample, false);
539*f7c14bbaSAndroid Build Coastguard Worker }
540*f7c14bbaSAndroid Build Coastguard Worker
user_ring_buffer__reserve(struct user_ring_buffer * rb,__u32 size)541*f7c14bbaSAndroid Build Coastguard Worker void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size)
542*f7c14bbaSAndroid Build Coastguard Worker {
543*f7c14bbaSAndroid Build Coastguard Worker __u32 avail_size, total_size, max_size;
544*f7c14bbaSAndroid Build Coastguard Worker /* 64-bit to avoid overflow in case of extreme application behavior */
545*f7c14bbaSAndroid Build Coastguard Worker __u64 cons_pos, prod_pos;
546*f7c14bbaSAndroid Build Coastguard Worker struct ringbuf_hdr *hdr;
547*f7c14bbaSAndroid Build Coastguard Worker
548*f7c14bbaSAndroid Build Coastguard Worker /* The top two bits are used as special flags */
549*f7c14bbaSAndroid Build Coastguard Worker if (size & (BPF_RINGBUF_BUSY_BIT | BPF_RINGBUF_DISCARD_BIT))
550*f7c14bbaSAndroid Build Coastguard Worker return errno = E2BIG, NULL;
551*f7c14bbaSAndroid Build Coastguard Worker
552*f7c14bbaSAndroid Build Coastguard Worker /* Synchronizes with smp_store_release() in __bpf_user_ringbuf_peek() in
553*f7c14bbaSAndroid Build Coastguard Worker * the kernel.
554*f7c14bbaSAndroid Build Coastguard Worker */
555*f7c14bbaSAndroid Build Coastguard Worker cons_pos = smp_load_acquire(rb->consumer_pos);
556*f7c14bbaSAndroid Build Coastguard Worker /* Synchronizes with smp_store_release() in user_ringbuf_commit() */
557*f7c14bbaSAndroid Build Coastguard Worker prod_pos = smp_load_acquire(rb->producer_pos);
558*f7c14bbaSAndroid Build Coastguard Worker
559*f7c14bbaSAndroid Build Coastguard Worker max_size = rb->mask + 1;
560*f7c14bbaSAndroid Build Coastguard Worker avail_size = max_size - (prod_pos - cons_pos);
561*f7c14bbaSAndroid Build Coastguard Worker /* Round up total size to a multiple of 8. */
562*f7c14bbaSAndroid Build Coastguard Worker total_size = (size + BPF_RINGBUF_HDR_SZ + 7) / 8 * 8;
563*f7c14bbaSAndroid Build Coastguard Worker
564*f7c14bbaSAndroid Build Coastguard Worker if (total_size > max_size)
565*f7c14bbaSAndroid Build Coastguard Worker return errno = E2BIG, NULL;
566*f7c14bbaSAndroid Build Coastguard Worker
567*f7c14bbaSAndroid Build Coastguard Worker if (avail_size < total_size)
568*f7c14bbaSAndroid Build Coastguard Worker return errno = ENOSPC, NULL;
569*f7c14bbaSAndroid Build Coastguard Worker
570*f7c14bbaSAndroid Build Coastguard Worker hdr = rb->data + (prod_pos & rb->mask);
571*f7c14bbaSAndroid Build Coastguard Worker hdr->len = size | BPF_RINGBUF_BUSY_BIT;
572*f7c14bbaSAndroid Build Coastguard Worker hdr->pad = 0;
573*f7c14bbaSAndroid Build Coastguard Worker
574*f7c14bbaSAndroid Build Coastguard Worker /* Synchronizes with smp_load_acquire() in __bpf_user_ringbuf_peek() in
575*f7c14bbaSAndroid Build Coastguard Worker * the kernel.
576*f7c14bbaSAndroid Build Coastguard Worker */
577*f7c14bbaSAndroid Build Coastguard Worker smp_store_release(rb->producer_pos, prod_pos + total_size);
578*f7c14bbaSAndroid Build Coastguard Worker
579*f7c14bbaSAndroid Build Coastguard Worker return (void *)rb->data + ((prod_pos + BPF_RINGBUF_HDR_SZ) & rb->mask);
580*f7c14bbaSAndroid Build Coastguard Worker }
581*f7c14bbaSAndroid Build Coastguard Worker
ns_elapsed_timespec(const struct timespec * start,const struct timespec * end)582*f7c14bbaSAndroid Build Coastguard Worker static __u64 ns_elapsed_timespec(const struct timespec *start, const struct timespec *end)
583*f7c14bbaSAndroid Build Coastguard Worker {
584*f7c14bbaSAndroid Build Coastguard Worker __u64 start_ns, end_ns, ns_per_s = 1000000000;
585*f7c14bbaSAndroid Build Coastguard Worker
586*f7c14bbaSAndroid Build Coastguard Worker start_ns = (__u64)start->tv_sec * ns_per_s + start->tv_nsec;
587*f7c14bbaSAndroid Build Coastguard Worker end_ns = (__u64)end->tv_sec * ns_per_s + end->tv_nsec;
588*f7c14bbaSAndroid Build Coastguard Worker
589*f7c14bbaSAndroid Build Coastguard Worker return end_ns - start_ns;
590*f7c14bbaSAndroid Build Coastguard Worker }
591*f7c14bbaSAndroid Build Coastguard Worker
user_ring_buffer__reserve_blocking(struct user_ring_buffer * rb,__u32 size,int timeout_ms)592*f7c14bbaSAndroid Build Coastguard Worker void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb, __u32 size, int timeout_ms)
593*f7c14bbaSAndroid Build Coastguard Worker {
594*f7c14bbaSAndroid Build Coastguard Worker void *sample;
595*f7c14bbaSAndroid Build Coastguard Worker int err, ms_remaining = timeout_ms;
596*f7c14bbaSAndroid Build Coastguard Worker struct timespec start;
597*f7c14bbaSAndroid Build Coastguard Worker
598*f7c14bbaSAndroid Build Coastguard Worker if (timeout_ms < 0 && timeout_ms != -1)
599*f7c14bbaSAndroid Build Coastguard Worker return errno = EINVAL, NULL;
600*f7c14bbaSAndroid Build Coastguard Worker
601*f7c14bbaSAndroid Build Coastguard Worker if (timeout_ms != -1) {
602*f7c14bbaSAndroid Build Coastguard Worker err = clock_gettime(CLOCK_MONOTONIC, &start);
603*f7c14bbaSAndroid Build Coastguard Worker if (err)
604*f7c14bbaSAndroid Build Coastguard Worker return NULL;
605*f7c14bbaSAndroid Build Coastguard Worker }
606*f7c14bbaSAndroid Build Coastguard Worker
607*f7c14bbaSAndroid Build Coastguard Worker do {
608*f7c14bbaSAndroid Build Coastguard Worker int cnt, ms_elapsed;
609*f7c14bbaSAndroid Build Coastguard Worker struct timespec curr;
610*f7c14bbaSAndroid Build Coastguard Worker __u64 ns_per_ms = 1000000;
611*f7c14bbaSAndroid Build Coastguard Worker
612*f7c14bbaSAndroid Build Coastguard Worker sample = user_ring_buffer__reserve(rb, size);
613*f7c14bbaSAndroid Build Coastguard Worker if (sample)
614*f7c14bbaSAndroid Build Coastguard Worker return sample;
615*f7c14bbaSAndroid Build Coastguard Worker else if (errno != ENOSPC)
616*f7c14bbaSAndroid Build Coastguard Worker return NULL;
617*f7c14bbaSAndroid Build Coastguard Worker
618*f7c14bbaSAndroid Build Coastguard Worker /* The kernel guarantees at least one event notification
619*f7c14bbaSAndroid Build Coastguard Worker * delivery whenever at least one sample is drained from the
620*f7c14bbaSAndroid Build Coastguard Worker * ring buffer in an invocation to bpf_ringbuf_drain(). Other
621*f7c14bbaSAndroid Build Coastguard Worker * additional events may be delivered at any time, but only one
622*f7c14bbaSAndroid Build Coastguard Worker * event is guaranteed per bpf_ringbuf_drain() invocation,
623*f7c14bbaSAndroid Build Coastguard Worker * provided that a sample is drained, and the BPF program did
624*f7c14bbaSAndroid Build Coastguard Worker * not pass BPF_RB_NO_WAKEUP to bpf_ringbuf_drain(). If
625*f7c14bbaSAndroid Build Coastguard Worker * BPF_RB_FORCE_WAKEUP is passed to bpf_ringbuf_drain(), a
626*f7c14bbaSAndroid Build Coastguard Worker * wakeup event will be delivered even if no samples are
627*f7c14bbaSAndroid Build Coastguard Worker * drained.
628*f7c14bbaSAndroid Build Coastguard Worker */
629*f7c14bbaSAndroid Build Coastguard Worker cnt = epoll_wait(rb->epoll_fd, &rb->event, 1, ms_remaining);
630*f7c14bbaSAndroid Build Coastguard Worker if (cnt < 0)
631*f7c14bbaSAndroid Build Coastguard Worker return NULL;
632*f7c14bbaSAndroid Build Coastguard Worker
633*f7c14bbaSAndroid Build Coastguard Worker if (timeout_ms == -1)
634*f7c14bbaSAndroid Build Coastguard Worker continue;
635*f7c14bbaSAndroid Build Coastguard Worker
636*f7c14bbaSAndroid Build Coastguard Worker err = clock_gettime(CLOCK_MONOTONIC, &curr);
637*f7c14bbaSAndroid Build Coastguard Worker if (err)
638*f7c14bbaSAndroid Build Coastguard Worker return NULL;
639*f7c14bbaSAndroid Build Coastguard Worker
640*f7c14bbaSAndroid Build Coastguard Worker ms_elapsed = ns_elapsed_timespec(&start, &curr) / ns_per_ms;
641*f7c14bbaSAndroid Build Coastguard Worker ms_remaining = timeout_ms - ms_elapsed;
642*f7c14bbaSAndroid Build Coastguard Worker } while (ms_remaining > 0);
643*f7c14bbaSAndroid Build Coastguard Worker
644*f7c14bbaSAndroid Build Coastguard Worker /* Try one more time to reserve a sample after the specified timeout has elapsed. */
645*f7c14bbaSAndroid Build Coastguard Worker return user_ring_buffer__reserve(rb, size);
646*f7c14bbaSAndroid Build Coastguard Worker }
647