8983a46f6580173774dc436adbe828403bbcdd3f
[linux-2.6-microblaze.git] / kernel / bpf / ringbuf.c
1 #include <linux/bpf.h>
2 #include <linux/btf.h>
3 #include <linux/err.h>
4 #include <linux/irq_work.h>
5 #include <linux/slab.h>
6 #include <linux/filter.h>
7 #include <linux/mm.h>
8 #include <linux/vmalloc.h>
9 #include <linux/wait.h>
10 #include <linux/poll.h>
11 #include <uapi/linux/btf.h>
12
13 #define RINGBUF_CREATE_FLAG_MASK (BPF_F_NUMA_NODE)
14
15 /* non-mmap()'able part of bpf_ringbuf (everything up to consumer page) */
16 #define RINGBUF_PGOFF \
17         (offsetof(struct bpf_ringbuf, consumer_pos) >> PAGE_SHIFT)
18 /* consumer page and producer page */
19 #define RINGBUF_POS_PAGES 2
20
21 #define RINGBUF_MAX_RECORD_SZ (UINT_MAX/4)
22
23 /* Maximum size of ring buffer area is limited by 32-bit page offset within
24  * record header, counted in pages. Reserve 8 bits for extensibility, and take
25  * into account few extra pages for consumer/producer pages and
26  * non-mmap()'able parts. This gives 64GB limit, which seems plenty for single
27  * ring buffer.
28  */
29 #define RINGBUF_MAX_DATA_SZ \
30         (((1ULL << 24) - RINGBUF_POS_PAGES - RINGBUF_PGOFF) * PAGE_SIZE)
31
32 struct bpf_ringbuf {
33         wait_queue_head_t waitq;
34         struct irq_work work;
35         u64 mask;
36         struct page **pages;
37         int nr_pages;
38         spinlock_t spinlock ____cacheline_aligned_in_smp;
39         /* Consumer and producer counters are put into separate pages to allow
40          * mapping consumer page as r/w, but restrict producer page to r/o.
41          * This protects producer position from being modified by user-space
42          * application and ruining in-kernel position tracking.
43          */
44         unsigned long consumer_pos __aligned(PAGE_SIZE);
45         unsigned long producer_pos __aligned(PAGE_SIZE);
46         char data[] __aligned(PAGE_SIZE);
47 };
48
49 struct bpf_ringbuf_map {
50         struct bpf_map map;
51         struct bpf_map_memory memory;
52         struct bpf_ringbuf *rb;
53 };
54
55 /* 8-byte ring buffer record header structure */
56 struct bpf_ringbuf_hdr {
57         u32 len;
58         u32 pg_off;
59 };
60
61 static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node)
62 {
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;
70         size_t array_size;
71         int i;
72
73         /* Each data page is mapped twice to allow "virtual"
74          * continuous read of samples wrapping around the end of ring
75          * buffer area:
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          * ------------------------------------------------------
81          * |            | TA             DA | TA             DA |
82          * ------------------------------------------------------
83          *                               ^^^^^^^
84          *                                  |
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.
89          */
90         array_size = (nr_meta_pages + 2 * nr_data_pages) * sizeof(*pages);
91         pages = bpf_map_area_alloc(array_size, numa_node);
92         if (!pages)
93                 return NULL;
94
95         for (i = 0; i < nr_pages; i++) {
96                 page = alloc_pages_node(numa_node, flags, 0);
97                 if (!page) {
98                         nr_pages = i;
99                         goto err_free_pages;
100                 }
101                 pages[i] = page;
102                 if (i >= nr_meta_pages)
103                         pages[nr_data_pages + i] = page;
104         }
105
106         rb = vmap(pages, nr_meta_pages + 2 * nr_data_pages,
107                   VM_ALLOC | VM_USERMAP, PAGE_KERNEL);
108         if (rb) {
109                 rb->pages = pages;
110                 rb->nr_pages = nr_pages;
111                 return rb;
112         }
113
114 err_free_pages:
115         for (i = 0; i < nr_pages; i++)
116                 __free_page(pages[i]);
117         kvfree(pages);
118         return NULL;
119 }
120
121 static void bpf_ringbuf_notify(struct irq_work *work)
122 {
123         struct bpf_ringbuf *rb = container_of(work, struct bpf_ringbuf, work);
124
125         wake_up_all(&rb->waitq);
126 }
127
128 static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node)
129 {
130         struct bpf_ringbuf *rb;
131
132         rb = bpf_ringbuf_area_alloc(data_sz, numa_node);
133         if (!rb)
134                 return ERR_PTR(-ENOMEM);
135
136         spin_lock_init(&rb->spinlock);
137         init_waitqueue_head(&rb->waitq);
138         init_irq_work(&rb->work, bpf_ringbuf_notify);
139
140         rb->mask = data_sz - 1;
141         rb->consumer_pos = 0;
142         rb->producer_pos = 0;
143
144         return rb;
145 }
146
147 static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr)
148 {
149         struct bpf_ringbuf_map *rb_map;
150         u64 cost;
151         int err;
152
153         if (attr->map_flags & ~RINGBUF_CREATE_FLAG_MASK)
154                 return ERR_PTR(-EINVAL);
155
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);
160
161 #ifdef CONFIG_64BIT
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);
165 #endif
166
167         rb_map = kzalloc(sizeof(*rb_map), GFP_USER | __GFP_ACCOUNT);
168         if (!rb_map)
169                 return ERR_PTR(-ENOMEM);
170
171         bpf_map_init_from_attr(&rb_map->map, attr);
172
173         cost = sizeof(struct bpf_ringbuf_map) +
174                sizeof(struct bpf_ringbuf) +
175                attr->max_entries;
176         err = bpf_map_charge_init(&rb_map->map.memory, cost);
177         if (err)
178                 goto err_free_map;
179
180         rb_map->rb = bpf_ringbuf_alloc(attr->max_entries, rb_map->map.numa_node);
181         if (IS_ERR(rb_map->rb)) {
182                 err = PTR_ERR(rb_map->rb);
183                 goto err_uncharge;
184         }
185
186         return &rb_map->map;
187
188 err_uncharge:
189         bpf_map_charge_finish(&rb_map->map.memory);
190 err_free_map:
191         kfree(rb_map);
192         return ERR_PTR(err);
193 }
194
195 static void bpf_ringbuf_free(struct bpf_ringbuf *rb)
196 {
197         /* copy pages pointer and nr_pages to local variable, as we are going
198          * to unmap rb itself with vunmap() below
199          */
200         struct page **pages = rb->pages;
201         int i, nr_pages = rb->nr_pages;
202
203         vunmap(rb);
204         for (i = 0; i < nr_pages; i++)
205                 __free_page(pages[i]);
206         kvfree(pages);
207 }
208
209 static void ringbuf_map_free(struct bpf_map *map)
210 {
211         struct bpf_ringbuf_map *rb_map;
212
213         rb_map = container_of(map, struct bpf_ringbuf_map, map);
214         bpf_ringbuf_free(rb_map->rb);
215         kfree(rb_map);
216 }
217
218 static void *ringbuf_map_lookup_elem(struct bpf_map *map, void *key)
219 {
220         return ERR_PTR(-ENOTSUPP);
221 }
222
223 static int ringbuf_map_update_elem(struct bpf_map *map, void *key, void *value,
224                                    u64 flags)
225 {
226         return -ENOTSUPP;
227 }
228
229 static int ringbuf_map_delete_elem(struct bpf_map *map, void *key)
230 {
231         return -ENOTSUPP;
232 }
233
234 static int ringbuf_map_get_next_key(struct bpf_map *map, void *key,
235                                     void *next_key)
236 {
237         return -ENOTSUPP;
238 }
239
240 static size_t bpf_ringbuf_mmap_page_cnt(const struct bpf_ringbuf *rb)
241 {
242         size_t data_pages = (rb->mask + 1) >> PAGE_SHIFT;
243
244         /* consumer page + producer page + 2 x data pages */
245         return RINGBUF_POS_PAGES + 2 * data_pages;
246 }
247
248 static int ringbuf_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
249 {
250         struct bpf_ringbuf_map *rb_map;
251         size_t mmap_sz;
252
253         rb_map = container_of(map, struct bpf_ringbuf_map, map);
254         mmap_sz = bpf_ringbuf_mmap_page_cnt(rb_map->rb) << PAGE_SHIFT;
255
256         if (vma->vm_pgoff * PAGE_SIZE + (vma->vm_end - vma->vm_start) > mmap_sz)
257                 return -EINVAL;
258
259         return remap_vmalloc_range(vma, rb_map->rb,
260                                    vma->vm_pgoff + RINGBUF_PGOFF);
261 }
262
263 static unsigned long ringbuf_avail_data_sz(struct bpf_ringbuf *rb)
264 {
265         unsigned long cons_pos, prod_pos;
266
267         cons_pos = smp_load_acquire(&rb->consumer_pos);
268         prod_pos = smp_load_acquire(&rb->producer_pos);
269         return prod_pos - cons_pos;
270 }
271
272 static __poll_t ringbuf_map_poll(struct bpf_map *map, struct file *filp,
273                                  struct poll_table_struct *pts)
274 {
275         struct bpf_ringbuf_map *rb_map;
276
277         rb_map = container_of(map, struct bpf_ringbuf_map, map);
278         poll_wait(filp, &rb_map->rb->waitq, pts);
279
280         if (ringbuf_avail_data_sz(rb_map->rb))
281                 return EPOLLIN | EPOLLRDNORM;
282         return 0;
283 }
284
285 static int ringbuf_map_btf_id;
286 const struct bpf_map_ops ringbuf_map_ops = {
287         .map_meta_equal = bpf_map_meta_equal,
288         .map_alloc = ringbuf_map_alloc,
289         .map_free = ringbuf_map_free,
290         .map_mmap = ringbuf_map_mmap,
291         .map_poll = ringbuf_map_poll,
292         .map_lookup_elem = ringbuf_map_lookup_elem,
293         .map_update_elem = ringbuf_map_update_elem,
294         .map_delete_elem = ringbuf_map_delete_elem,
295         .map_get_next_key = ringbuf_map_get_next_key,
296         .map_btf_name = "bpf_ringbuf_map",
297         .map_btf_id = &ringbuf_map_btf_id,
298 };
299
300 /* Given pointer to ring buffer record metadata and struct bpf_ringbuf itself,
301  * calculate offset from record metadata to ring buffer in pages, rounded
302  * down. This page offset is stored as part of record metadata and allows to
303  * restore struct bpf_ringbuf * from record pointer. This page offset is
304  * stored at offset 4 of record metadata header.
305  */
306 static size_t bpf_ringbuf_rec_pg_off(struct bpf_ringbuf *rb,
307                                      struct bpf_ringbuf_hdr *hdr)
308 {
309         return ((void *)hdr - (void *)rb) >> PAGE_SHIFT;
310 }
311
312 /* Given pointer to ring buffer record header, restore pointer to struct
313  * bpf_ringbuf itself by using page offset stored at offset 4
314  */
315 static struct bpf_ringbuf *
316 bpf_ringbuf_restore_from_rec(struct bpf_ringbuf_hdr *hdr)
317 {
318         unsigned long addr = (unsigned long)(void *)hdr;
319         unsigned long off = (unsigned long)hdr->pg_off << PAGE_SHIFT;
320
321         return (void*)((addr & PAGE_MASK) - off);
322 }
323
324 static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
325 {
326         unsigned long cons_pos, prod_pos, new_prod_pos, flags;
327         u32 len, pg_off;
328         struct bpf_ringbuf_hdr *hdr;
329
330         if (unlikely(size > RINGBUF_MAX_RECORD_SZ))
331                 return NULL;
332
333         len = round_up(size + BPF_RINGBUF_HDR_SZ, 8);
334         cons_pos = smp_load_acquire(&rb->consumer_pos);
335
336         if (in_nmi()) {
337                 if (!spin_trylock_irqsave(&rb->spinlock, flags))
338                         return NULL;
339         } else {
340                 spin_lock_irqsave(&rb->spinlock, flags);
341         }
342
343         prod_pos = rb->producer_pos;
344         new_prod_pos = prod_pos + len;
345
346         /* check for out of ringbuf space by ensuring producer position
347          * doesn't advance more than (ringbuf_size - 1) ahead
348          */
349         if (new_prod_pos - cons_pos > rb->mask) {
350                 spin_unlock_irqrestore(&rb->spinlock, flags);
351                 return NULL;
352         }
353
354         hdr = (void *)rb->data + (prod_pos & rb->mask);
355         pg_off = bpf_ringbuf_rec_pg_off(rb, hdr);
356         hdr->len = size | BPF_RINGBUF_BUSY_BIT;
357         hdr->pg_off = pg_off;
358
359         /* pairs with consumer's smp_load_acquire() */
360         smp_store_release(&rb->producer_pos, new_prod_pos);
361
362         spin_unlock_irqrestore(&rb->spinlock, flags);
363
364         return (void *)hdr + BPF_RINGBUF_HDR_SZ;
365 }
366
367 BPF_CALL_3(bpf_ringbuf_reserve, struct bpf_map *, map, u64, size, u64, flags)
368 {
369         struct bpf_ringbuf_map *rb_map;
370
371         if (unlikely(flags))
372                 return 0;
373
374         rb_map = container_of(map, struct bpf_ringbuf_map, map);
375         return (unsigned long)__bpf_ringbuf_reserve(rb_map->rb, size);
376 }
377
378 const struct bpf_func_proto bpf_ringbuf_reserve_proto = {
379         .func           = bpf_ringbuf_reserve,
380         .ret_type       = RET_PTR_TO_ALLOC_MEM_OR_NULL,
381         .arg1_type      = ARG_CONST_MAP_PTR,
382         .arg2_type      = ARG_CONST_ALLOC_SIZE_OR_ZERO,
383         .arg3_type      = ARG_ANYTHING,
384 };
385
386 static void bpf_ringbuf_commit(void *sample, u64 flags, bool discard)
387 {
388         unsigned long rec_pos, cons_pos;
389         struct bpf_ringbuf_hdr *hdr;
390         struct bpf_ringbuf *rb;
391         u32 new_len;
392
393         hdr = sample - BPF_RINGBUF_HDR_SZ;
394         rb = bpf_ringbuf_restore_from_rec(hdr);
395         new_len = hdr->len ^ BPF_RINGBUF_BUSY_BIT;
396         if (discard)
397                 new_len |= BPF_RINGBUF_DISCARD_BIT;
398
399         /* update record header with correct final size prefix */
400         xchg(&hdr->len, new_len);
401
402         /* if consumer caught up and is waiting for our record, notify about
403          * new data availability
404          */
405         rec_pos = (void *)hdr - (void *)rb->data;
406         cons_pos = smp_load_acquire(&rb->consumer_pos) & rb->mask;
407
408         if (flags & BPF_RB_FORCE_WAKEUP)
409                 irq_work_queue(&rb->work);
410         else if (cons_pos == rec_pos && !(flags & BPF_RB_NO_WAKEUP))
411                 irq_work_queue(&rb->work);
412 }
413
414 BPF_CALL_2(bpf_ringbuf_submit, void *, sample, u64, flags)
415 {
416         bpf_ringbuf_commit(sample, flags, false /* discard */);
417         return 0;
418 }
419
420 const struct bpf_func_proto bpf_ringbuf_submit_proto = {
421         .func           = bpf_ringbuf_submit,
422         .ret_type       = RET_VOID,
423         .arg1_type      = ARG_PTR_TO_ALLOC_MEM,
424         .arg2_type      = ARG_ANYTHING,
425 };
426
427 BPF_CALL_2(bpf_ringbuf_discard, void *, sample, u64, flags)
428 {
429         bpf_ringbuf_commit(sample, flags, true /* discard */);
430         return 0;
431 }
432
433 const struct bpf_func_proto bpf_ringbuf_discard_proto = {
434         .func           = bpf_ringbuf_discard,
435         .ret_type       = RET_VOID,
436         .arg1_type      = ARG_PTR_TO_ALLOC_MEM,
437         .arg2_type      = ARG_ANYTHING,
438 };
439
440 BPF_CALL_4(bpf_ringbuf_output, struct bpf_map *, map, void *, data, u64, size,
441            u64, flags)
442 {
443         struct bpf_ringbuf_map *rb_map;
444         void *rec;
445
446         if (unlikely(flags & ~(BPF_RB_NO_WAKEUP | BPF_RB_FORCE_WAKEUP)))
447                 return -EINVAL;
448
449         rb_map = container_of(map, struct bpf_ringbuf_map, map);
450         rec = __bpf_ringbuf_reserve(rb_map->rb, size);
451         if (!rec)
452                 return -EAGAIN;
453
454         memcpy(rec, data, size);
455         bpf_ringbuf_commit(rec, flags, false /* discard */);
456         return 0;
457 }
458
459 const struct bpf_func_proto bpf_ringbuf_output_proto = {
460         .func           = bpf_ringbuf_output,
461         .ret_type       = RET_INTEGER,
462         .arg1_type      = ARG_CONST_MAP_PTR,
463         .arg2_type      = ARG_PTR_TO_MEM,
464         .arg3_type      = ARG_CONST_SIZE_OR_ZERO,
465         .arg4_type      = ARG_ANYTHING,
466 };
467
468 BPF_CALL_2(bpf_ringbuf_query, struct bpf_map *, map, u64, flags)
469 {
470         struct bpf_ringbuf *rb;
471
472         rb = container_of(map, struct bpf_ringbuf_map, map)->rb;
473
474         switch (flags) {
475         case BPF_RB_AVAIL_DATA:
476                 return ringbuf_avail_data_sz(rb);
477         case BPF_RB_RING_SIZE:
478                 return rb->mask + 1;
479         case BPF_RB_CONS_POS:
480                 return smp_load_acquire(&rb->consumer_pos);
481         case BPF_RB_PROD_POS:
482                 return smp_load_acquire(&rb->producer_pos);
483         default:
484                 return 0;
485         }
486 }
487
488 const struct bpf_func_proto bpf_ringbuf_query_proto = {
489         .func           = bpf_ringbuf_query,
490         .ret_type       = RET_INTEGER,
491         .arg1_type      = ARG_CONST_MAP_PTR,
492         .arg2_type      = ARG_ANYTHING,
493 };