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>
13 #include <linux/btf_ids.h>
15 #define RINGBUF_CREATE_FLAG_MASK (BPF_F_NUMA_NODE)
17 /* non-mmap()'able part of bpf_ringbuf (everything up to consumer page) */
18 #define RINGBUF_PGOFF \
19 (offsetof(struct bpf_ringbuf, consumer_pos) >> PAGE_SHIFT)
20 /* consumer page and producer page */
21 #define RINGBUF_POS_PAGES 2
23 #define RINGBUF_MAX_RECORD_SZ (UINT_MAX/4)
25 /* Maximum size of ring buffer area is limited by 32-bit page offset within
26 * record header, counted in pages. Reserve 8 bits for extensibility, and take
27 * into account few extra pages for consumer/producer pages and
28 * non-mmap()'able parts. This gives 64GB limit, which seems plenty for single
31 #define RINGBUF_MAX_DATA_SZ \
32 (((1ULL << 24) - RINGBUF_POS_PAGES - RINGBUF_PGOFF) * PAGE_SIZE)
35 wait_queue_head_t waitq;
40 spinlock_t spinlock ____cacheline_aligned_in_smp;
41 /* Consumer and producer counters are put into separate pages to allow
42 * mapping consumer page as r/w, but restrict producer page to r/o.
43 * This protects producer position from being modified by user-space
44 * application and ruining in-kernel position tracking.
46 unsigned long consumer_pos __aligned(PAGE_SIZE);
47 unsigned long producer_pos __aligned(PAGE_SIZE);
48 char data[] __aligned(PAGE_SIZE);
51 struct bpf_ringbuf_map {
53 struct bpf_ringbuf *rb;
56 /* 8-byte ring buffer record header structure */
57 struct bpf_ringbuf_hdr {
62 static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node)
64 const gfp_t flags = GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL |
65 __GFP_NOWARN | __GFP_ZERO;
66 int nr_meta_pages = RINGBUF_PGOFF + RINGBUF_POS_PAGES;
67 int nr_data_pages = data_sz >> PAGE_SHIFT;
68 int nr_pages = nr_meta_pages + nr_data_pages;
69 struct page **pages, *page;
70 struct bpf_ringbuf *rb;
74 /* Each data page is mapped twice to allow "virtual"
75 * continuous read of samples wrapping around the end of ring
77 * ------------------------------------------------------
78 * | meta pages | real data pages | same data pages |
79 * ------------------------------------------------------
80 * | | 1 2 3 4 5 6 7 8 9 | 1 2 3 4 5 6 7 8 9 |
81 * ------------------------------------------------------
83 * ------------------------------------------------------
86 * Here, no need to worry about special handling of wrapped-around
87 * data due to double-mapped data pages. This works both in kernel and
88 * when mmap()'ed in user-space, simplifying both kernel and
89 * user-space implementations significantly.
91 array_size = (nr_meta_pages + 2 * nr_data_pages) * sizeof(*pages);
92 pages = bpf_map_area_alloc(array_size, numa_node);
96 for (i = 0; i < nr_pages; i++) {
97 page = alloc_pages_node(numa_node, flags, 0);
103 if (i >= nr_meta_pages)
104 pages[nr_data_pages + i] = page;
107 rb = vmap(pages, nr_meta_pages + 2 * nr_data_pages,
108 VM_MAP | VM_USERMAP, PAGE_KERNEL);
110 kmemleak_not_leak(pages);
112 rb->nr_pages = nr_pages;
117 for (i = 0; i < nr_pages; i++)
118 __free_page(pages[i]);
123 static void bpf_ringbuf_notify(struct irq_work *work)
125 struct bpf_ringbuf *rb = container_of(work, struct bpf_ringbuf, work);
127 wake_up_all(&rb->waitq);
130 static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node)
132 struct bpf_ringbuf *rb;
134 rb = bpf_ringbuf_area_alloc(data_sz, numa_node);
138 spin_lock_init(&rb->spinlock);
139 init_waitqueue_head(&rb->waitq);
140 init_irq_work(&rb->work, bpf_ringbuf_notify);
142 rb->mask = data_sz - 1;
143 rb->consumer_pos = 0;
144 rb->producer_pos = 0;
149 static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr)
151 struct bpf_ringbuf_map *rb_map;
153 if (attr->map_flags & ~RINGBUF_CREATE_FLAG_MASK)
154 return ERR_PTR(-EINVAL);
156 if (attr->key_size || attr->value_size ||
157 !is_power_of_2(attr->max_entries) ||
158 !PAGE_ALIGNED(attr->max_entries))
159 return ERR_PTR(-EINVAL);
162 /* on 32-bit arch, it's impossible to overflow record's hdr->pgoff */
163 if (attr->max_entries > RINGBUF_MAX_DATA_SZ)
164 return ERR_PTR(-E2BIG);
167 rb_map = kzalloc(sizeof(*rb_map), GFP_USER | __GFP_ACCOUNT);
169 return ERR_PTR(-ENOMEM);
171 bpf_map_init_from_attr(&rb_map->map, attr);
173 rb_map->rb = bpf_ringbuf_alloc(attr->max_entries, rb_map->map.numa_node);
176 return ERR_PTR(-ENOMEM);
182 static void bpf_ringbuf_free(struct bpf_ringbuf *rb)
184 /* copy pages pointer and nr_pages to local variable, as we are going
185 * to unmap rb itself with vunmap() below
187 struct page **pages = rb->pages;
188 int i, nr_pages = rb->nr_pages;
191 for (i = 0; i < nr_pages; i++)
192 __free_page(pages[i]);
196 static void ringbuf_map_free(struct bpf_map *map)
198 struct bpf_ringbuf_map *rb_map;
200 rb_map = container_of(map, struct bpf_ringbuf_map, map);
201 bpf_ringbuf_free(rb_map->rb);
205 static void *ringbuf_map_lookup_elem(struct bpf_map *map, void *key)
207 return ERR_PTR(-ENOTSUPP);
210 static int ringbuf_map_update_elem(struct bpf_map *map, void *key, void *value,
216 static int ringbuf_map_delete_elem(struct bpf_map *map, void *key)
221 static int ringbuf_map_get_next_key(struct bpf_map *map, void *key,
227 static int ringbuf_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
229 struct bpf_ringbuf_map *rb_map;
231 rb_map = container_of(map, struct bpf_ringbuf_map, map);
233 if (vma->vm_flags & VM_WRITE) {
234 /* allow writable mapping for the consumer_pos only */
235 if (vma->vm_pgoff != 0 || vma->vm_end - vma->vm_start != PAGE_SIZE)
238 vma->vm_flags &= ~VM_MAYWRITE;
240 /* remap_vmalloc_range() checks size and offset constraints */
241 return remap_vmalloc_range(vma, rb_map->rb,
242 vma->vm_pgoff + RINGBUF_PGOFF);
245 static unsigned long ringbuf_avail_data_sz(struct bpf_ringbuf *rb)
247 unsigned long cons_pos, prod_pos;
249 cons_pos = smp_load_acquire(&rb->consumer_pos);
250 prod_pos = smp_load_acquire(&rb->producer_pos);
251 return prod_pos - cons_pos;
254 static __poll_t ringbuf_map_poll(struct bpf_map *map, struct file *filp,
255 struct poll_table_struct *pts)
257 struct bpf_ringbuf_map *rb_map;
259 rb_map = container_of(map, struct bpf_ringbuf_map, map);
260 poll_wait(filp, &rb_map->rb->waitq, pts);
262 if (ringbuf_avail_data_sz(rb_map->rb))
263 return EPOLLIN | EPOLLRDNORM;
267 BTF_ID_LIST_SINGLE(ringbuf_map_btf_ids, struct, bpf_ringbuf_map)
268 const struct bpf_map_ops ringbuf_map_ops = {
269 .map_meta_equal = bpf_map_meta_equal,
270 .map_alloc = ringbuf_map_alloc,
271 .map_free = ringbuf_map_free,
272 .map_mmap = ringbuf_map_mmap,
273 .map_poll = ringbuf_map_poll,
274 .map_lookup_elem = ringbuf_map_lookup_elem,
275 .map_update_elem = ringbuf_map_update_elem,
276 .map_delete_elem = ringbuf_map_delete_elem,
277 .map_get_next_key = ringbuf_map_get_next_key,
278 .map_btf_id = &ringbuf_map_btf_ids[0],
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 | OBJ_RELEASE,
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 | OBJ_RELEASE,
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 | MEM_RDONLY,
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,
479 BPF_CALL_4(bpf_ringbuf_reserve_dynptr, struct bpf_map *, map, u32, size, u64, flags,
480 struct bpf_dynptr_kern *, ptr)
482 struct bpf_ringbuf_map *rb_map;
486 if (unlikely(flags)) {
487 bpf_dynptr_set_null(ptr);
491 err = bpf_dynptr_check_size(size);
493 bpf_dynptr_set_null(ptr);
497 rb_map = container_of(map, struct bpf_ringbuf_map, map);
499 sample = __bpf_ringbuf_reserve(rb_map->rb, size);
501 bpf_dynptr_set_null(ptr);
505 bpf_dynptr_init(ptr, sample, BPF_DYNPTR_TYPE_RINGBUF, 0, size);
510 const struct bpf_func_proto bpf_ringbuf_reserve_dynptr_proto = {
511 .func = bpf_ringbuf_reserve_dynptr,
512 .ret_type = RET_INTEGER,
513 .arg1_type = ARG_CONST_MAP_PTR,
514 .arg2_type = ARG_ANYTHING,
515 .arg3_type = ARG_ANYTHING,
516 .arg4_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_RINGBUF | MEM_UNINIT,
519 BPF_CALL_2(bpf_ringbuf_submit_dynptr, struct bpf_dynptr_kern *, ptr, u64, flags)
524 bpf_ringbuf_commit(ptr->data, flags, false /* discard */);
526 bpf_dynptr_set_null(ptr);
531 const struct bpf_func_proto bpf_ringbuf_submit_dynptr_proto = {
532 .func = bpf_ringbuf_submit_dynptr,
533 .ret_type = RET_VOID,
534 .arg1_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_RINGBUF | OBJ_RELEASE,
535 .arg2_type = ARG_ANYTHING,
538 BPF_CALL_2(bpf_ringbuf_discard_dynptr, struct bpf_dynptr_kern *, ptr, u64, flags)
543 bpf_ringbuf_commit(ptr->data, flags, true /* discard */);
545 bpf_dynptr_set_null(ptr);
550 const struct bpf_func_proto bpf_ringbuf_discard_dynptr_proto = {
551 .func = bpf_ringbuf_discard_dynptr,
552 .ret_type = RET_VOID,
553 .arg1_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_RINGBUF | OBJ_RELEASE,
554 .arg2_type = ARG_ANYTHING,