4 #include <linux/irq_work.h>
5 #include <linux/slab.h>
6 #include <linux/filter.h>
8 #include <linux/vmalloc.h>
9 #include <linux/wait.h>
10 #include <linux/poll.h>
11 #include <linux/kmemleak.h>
12 #include <uapi/linux/btf.h>
14 #define RINGBUF_CREATE_FLAG_MASK (BPF_F_NUMA_NODE)
16 /* non-mmap()'able part of bpf_ringbuf (everything up to consumer page) */
17 #define RINGBUF_PGOFF \
18 (offsetof(struct bpf_ringbuf, consumer_pos) >> PAGE_SHIFT)
19 /* consumer page and producer page */
20 #define RINGBUF_POS_PAGES 2
22 #define RINGBUF_MAX_RECORD_SZ (UINT_MAX/4)
24 /* Maximum size of ring buffer area is limited by 32-bit page offset within
25 * record header, counted in pages. Reserve 8 bits for extensibility, and take
26 * into account few extra pages for consumer/producer pages and
27 * non-mmap()'able parts. This gives 64GB limit, which seems plenty for single
30 #define RINGBUF_MAX_DATA_SZ \
31 (((1ULL << 24) - RINGBUF_POS_PAGES - RINGBUF_PGOFF) * PAGE_SIZE)
34 wait_queue_head_t waitq;
39 spinlock_t spinlock ____cacheline_aligned_in_smp;
40 /* Consumer and producer counters are put into separate pages to allow
41 * mapping consumer page as r/w, but restrict producer page to r/o.
42 * This protects producer position from being modified by user-space
43 * application and ruining in-kernel position tracking.
45 unsigned long consumer_pos __aligned(PAGE_SIZE);
46 unsigned long producer_pos __aligned(PAGE_SIZE);
47 char data[] __aligned(PAGE_SIZE);
50 struct bpf_ringbuf_map {
52 struct bpf_ringbuf *rb;
55 /* 8-byte ring buffer record header structure */
56 struct bpf_ringbuf_hdr {
61 static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node)
63 const gfp_t flags = GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL |
64 __GFP_NOWARN | __GFP_ZERO;
65 int nr_meta_pages = RINGBUF_PGOFF + RINGBUF_POS_PAGES;
66 int nr_data_pages = data_sz >> PAGE_SHIFT;
67 int nr_pages = nr_meta_pages + nr_data_pages;
68 struct page **pages, *page;
69 struct bpf_ringbuf *rb;
73 /* Each data page is mapped twice to allow "virtual"
74 * continuous read of samples wrapping around the end of ring
76 * ------------------------------------------------------
77 * | meta pages | real data pages | same data pages |
78 * ------------------------------------------------------
79 * | | 1 2 3 4 5 6 7 8 9 | 1 2 3 4 5 6 7 8 9 |
80 * ------------------------------------------------------
82 * ------------------------------------------------------
85 * Here, no need to worry about special handling of wrapped-around
86 * data due to double-mapped data pages. This works both in kernel and
87 * when mmap()'ed in user-space, simplifying both kernel and
88 * user-space implementations significantly.
90 array_size = (nr_meta_pages + 2 * nr_data_pages) * sizeof(*pages);
91 pages = bpf_map_area_alloc(array_size, numa_node);
95 for (i = 0; i < nr_pages; i++) {
96 page = alloc_pages_node(numa_node, flags, 0);
102 if (i >= nr_meta_pages)
103 pages[nr_data_pages + i] = page;
106 rb = vmap(pages, nr_meta_pages + 2 * nr_data_pages,
107 VM_ALLOC | VM_USERMAP, PAGE_KERNEL);
109 kmemleak_not_leak(pages);
111 rb->nr_pages = nr_pages;
116 for (i = 0; i < nr_pages; i++)
117 __free_page(pages[i]);
122 static void bpf_ringbuf_notify(struct irq_work *work)
124 struct bpf_ringbuf *rb = container_of(work, struct bpf_ringbuf, work);
126 wake_up_all(&rb->waitq);
129 static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node)
131 struct bpf_ringbuf *rb;
133 rb = bpf_ringbuf_area_alloc(data_sz, numa_node);
137 spin_lock_init(&rb->spinlock);
138 init_waitqueue_head(&rb->waitq);
139 init_irq_work(&rb->work, bpf_ringbuf_notify);
141 rb->mask = data_sz - 1;
142 rb->consumer_pos = 0;
143 rb->producer_pos = 0;
148 static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr)
150 struct bpf_ringbuf_map *rb_map;
152 if (attr->map_flags & ~RINGBUF_CREATE_FLAG_MASK)
153 return ERR_PTR(-EINVAL);
155 if (attr->key_size || attr->value_size ||
156 !is_power_of_2(attr->max_entries) ||
157 !PAGE_ALIGNED(attr->max_entries))
158 return ERR_PTR(-EINVAL);
161 /* on 32-bit arch, it's impossible to overflow record's hdr->pgoff */
162 if (attr->max_entries > RINGBUF_MAX_DATA_SZ)
163 return ERR_PTR(-E2BIG);
166 rb_map = kzalloc(sizeof(*rb_map), GFP_USER | __GFP_ACCOUNT);
168 return ERR_PTR(-ENOMEM);
170 bpf_map_init_from_attr(&rb_map->map, attr);
172 rb_map->rb = bpf_ringbuf_alloc(attr->max_entries, rb_map->map.numa_node);
175 return ERR_PTR(-ENOMEM);
181 static void bpf_ringbuf_free(struct bpf_ringbuf *rb)
183 /* copy pages pointer and nr_pages to local variable, as we are going
184 * to unmap rb itself with vunmap() below
186 struct page **pages = rb->pages;
187 int i, nr_pages = rb->nr_pages;
190 for (i = 0; i < nr_pages; i++)
191 __free_page(pages[i]);
195 static void ringbuf_map_free(struct bpf_map *map)
197 struct bpf_ringbuf_map *rb_map;
199 rb_map = container_of(map, struct bpf_ringbuf_map, map);
200 bpf_ringbuf_free(rb_map->rb);
204 static void *ringbuf_map_lookup_elem(struct bpf_map *map, void *key)
206 return ERR_PTR(-ENOTSUPP);
209 static int ringbuf_map_update_elem(struct bpf_map *map, void *key, void *value,
215 static int ringbuf_map_delete_elem(struct bpf_map *map, void *key)
220 static int ringbuf_map_get_next_key(struct bpf_map *map, void *key,
226 static int ringbuf_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
228 struct bpf_ringbuf_map *rb_map;
230 rb_map = container_of(map, struct bpf_ringbuf_map, map);
232 if (vma->vm_flags & VM_WRITE) {
233 /* allow writable mapping for the consumer_pos only */
234 if (vma->vm_pgoff != 0 || vma->vm_end - vma->vm_start != PAGE_SIZE)
237 vma->vm_flags &= ~VM_MAYWRITE;
239 /* remap_vmalloc_range() checks size and offset constraints */
240 return remap_vmalloc_range(vma, rb_map->rb,
241 vma->vm_pgoff + RINGBUF_PGOFF);
244 static unsigned long ringbuf_avail_data_sz(struct bpf_ringbuf *rb)
246 unsigned long cons_pos, prod_pos;
248 cons_pos = smp_load_acquire(&rb->consumer_pos);
249 prod_pos = smp_load_acquire(&rb->producer_pos);
250 return prod_pos - cons_pos;
253 static __poll_t ringbuf_map_poll(struct bpf_map *map, struct file *filp,
254 struct poll_table_struct *pts)
256 struct bpf_ringbuf_map *rb_map;
258 rb_map = container_of(map, struct bpf_ringbuf_map, map);
259 poll_wait(filp, &rb_map->rb->waitq, pts);
261 if (ringbuf_avail_data_sz(rb_map->rb))
262 return EPOLLIN | EPOLLRDNORM;
266 static int ringbuf_map_btf_id;
267 const struct bpf_map_ops ringbuf_map_ops = {
268 .map_meta_equal = bpf_map_meta_equal,
269 .map_alloc = ringbuf_map_alloc,
270 .map_free = ringbuf_map_free,
271 .map_mmap = ringbuf_map_mmap,
272 .map_poll = ringbuf_map_poll,
273 .map_lookup_elem = ringbuf_map_lookup_elem,
274 .map_update_elem = ringbuf_map_update_elem,
275 .map_delete_elem = ringbuf_map_delete_elem,
276 .map_get_next_key = ringbuf_map_get_next_key,
277 .map_btf_name = "bpf_ringbuf_map",
278 .map_btf_id = &ringbuf_map_btf_id,
281 /* Given pointer to ring buffer record metadata and struct bpf_ringbuf itself,
282 * calculate offset from record metadata to ring buffer in pages, rounded
283 * down. This page offset is stored as part of record metadata and allows to
284 * restore struct bpf_ringbuf * from record pointer. This page offset is
285 * stored at offset 4 of record metadata header.
287 static size_t bpf_ringbuf_rec_pg_off(struct bpf_ringbuf *rb,
288 struct bpf_ringbuf_hdr *hdr)
290 return ((void *)hdr - (void *)rb) >> PAGE_SHIFT;
293 /* Given pointer to ring buffer record header, restore pointer to struct
294 * bpf_ringbuf itself by using page offset stored at offset 4
296 static struct bpf_ringbuf *
297 bpf_ringbuf_restore_from_rec(struct bpf_ringbuf_hdr *hdr)
299 unsigned long addr = (unsigned long)(void *)hdr;
300 unsigned long off = (unsigned long)hdr->pg_off << PAGE_SHIFT;
302 return (void*)((addr & PAGE_MASK) - off);
305 static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
307 unsigned long cons_pos, prod_pos, new_prod_pos, flags;
309 struct bpf_ringbuf_hdr *hdr;
311 if (unlikely(size > RINGBUF_MAX_RECORD_SZ))
314 len = round_up(size + BPF_RINGBUF_HDR_SZ, 8);
315 if (len > rb->mask + 1)
318 cons_pos = smp_load_acquire(&rb->consumer_pos);
321 if (!spin_trylock_irqsave(&rb->spinlock, flags))
324 spin_lock_irqsave(&rb->spinlock, flags);
327 prod_pos = rb->producer_pos;
328 new_prod_pos = prod_pos + len;
330 /* check for out of ringbuf space by ensuring producer position
331 * doesn't advance more than (ringbuf_size - 1) ahead
333 if (new_prod_pos - cons_pos > rb->mask) {
334 spin_unlock_irqrestore(&rb->spinlock, flags);
338 hdr = (void *)rb->data + (prod_pos & rb->mask);
339 pg_off = bpf_ringbuf_rec_pg_off(rb, hdr);
340 hdr->len = size | BPF_RINGBUF_BUSY_BIT;
341 hdr->pg_off = pg_off;
343 /* pairs with consumer's smp_load_acquire() */
344 smp_store_release(&rb->producer_pos, new_prod_pos);
346 spin_unlock_irqrestore(&rb->spinlock, flags);
348 return (void *)hdr + BPF_RINGBUF_HDR_SZ;
351 BPF_CALL_3(bpf_ringbuf_reserve, struct bpf_map *, map, u64, size, u64, flags)
353 struct bpf_ringbuf_map *rb_map;
358 rb_map = container_of(map, struct bpf_ringbuf_map, map);
359 return (unsigned long)__bpf_ringbuf_reserve(rb_map->rb, size);
362 const struct bpf_func_proto bpf_ringbuf_reserve_proto = {
363 .func = bpf_ringbuf_reserve,
364 .ret_type = RET_PTR_TO_ALLOC_MEM_OR_NULL,
365 .arg1_type = ARG_CONST_MAP_PTR,
366 .arg2_type = ARG_CONST_ALLOC_SIZE_OR_ZERO,
367 .arg3_type = ARG_ANYTHING,
370 static void bpf_ringbuf_commit(void *sample, u64 flags, bool discard)
372 unsigned long rec_pos, cons_pos;
373 struct bpf_ringbuf_hdr *hdr;
374 struct bpf_ringbuf *rb;
377 hdr = sample - BPF_RINGBUF_HDR_SZ;
378 rb = bpf_ringbuf_restore_from_rec(hdr);
379 new_len = hdr->len ^ BPF_RINGBUF_BUSY_BIT;
381 new_len |= BPF_RINGBUF_DISCARD_BIT;
383 /* update record header with correct final size prefix */
384 xchg(&hdr->len, new_len);
386 /* if consumer caught up and is waiting for our record, notify about
387 * new data availability
389 rec_pos = (void *)hdr - (void *)rb->data;
390 cons_pos = smp_load_acquire(&rb->consumer_pos) & rb->mask;
392 if (flags & BPF_RB_FORCE_WAKEUP)
393 irq_work_queue(&rb->work);
394 else if (cons_pos == rec_pos && !(flags & BPF_RB_NO_WAKEUP))
395 irq_work_queue(&rb->work);
398 BPF_CALL_2(bpf_ringbuf_submit, void *, sample, u64, flags)
400 bpf_ringbuf_commit(sample, flags, false /* discard */);
404 const struct bpf_func_proto bpf_ringbuf_submit_proto = {
405 .func = bpf_ringbuf_submit,
406 .ret_type = RET_VOID,
407 .arg1_type = ARG_PTR_TO_ALLOC_MEM,
408 .arg2_type = ARG_ANYTHING,
411 BPF_CALL_2(bpf_ringbuf_discard, void *, sample, u64, flags)
413 bpf_ringbuf_commit(sample, flags, true /* discard */);
417 const struct bpf_func_proto bpf_ringbuf_discard_proto = {
418 .func = bpf_ringbuf_discard,
419 .ret_type = RET_VOID,
420 .arg1_type = ARG_PTR_TO_ALLOC_MEM,
421 .arg2_type = ARG_ANYTHING,
424 BPF_CALL_4(bpf_ringbuf_output, struct bpf_map *, map, void *, data, u64, size,
427 struct bpf_ringbuf_map *rb_map;
430 if (unlikely(flags & ~(BPF_RB_NO_WAKEUP | BPF_RB_FORCE_WAKEUP)))
433 rb_map = container_of(map, struct bpf_ringbuf_map, map);
434 rec = __bpf_ringbuf_reserve(rb_map->rb, size);
438 memcpy(rec, data, size);
439 bpf_ringbuf_commit(rec, flags, false /* discard */);
443 const struct bpf_func_proto bpf_ringbuf_output_proto = {
444 .func = bpf_ringbuf_output,
445 .ret_type = RET_INTEGER,
446 .arg1_type = ARG_CONST_MAP_PTR,
447 .arg2_type = ARG_PTR_TO_MEM,
448 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
449 .arg4_type = ARG_ANYTHING,
452 BPF_CALL_2(bpf_ringbuf_query, struct bpf_map *, map, u64, flags)
454 struct bpf_ringbuf *rb;
456 rb = container_of(map, struct bpf_ringbuf_map, map)->rb;
459 case BPF_RB_AVAIL_DATA:
460 return ringbuf_avail_data_sz(rb);
461 case BPF_RB_RING_SIZE:
463 case BPF_RB_CONS_POS:
464 return smp_load_acquire(&rb->consumer_pos);
465 case BPF_RB_PROD_POS:
466 return smp_load_acquire(&rb->producer_pos);
472 const struct bpf_func_proto bpf_ringbuf_query_proto = {
473 .func = bpf_ringbuf_query,
474 .ret_type = RET_INTEGER,
475 .arg1_type = ARG_CONST_MAP_PTR,
476 .arg2_type = ARG_ANYTHING,