bpf: Disable bh in bpf_test_run for xdp and tc prog
[linux-2.6-microblaze.git] / net / bpf / test_run.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
3  */
4 #include <linux/bpf.h>
5 #include <linux/btf.h>
6 #include <linux/btf_ids.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
9 #include <linux/vmalloc.h>
10 #include <linux/etherdevice.h>
11 #include <linux/filter.h>
12 #include <linux/rcupdate_trace.h>
13 #include <linux/sched/signal.h>
14 #include <net/bpf_sk_storage.h>
15 #include <net/sock.h>
16 #include <net/tcp.h>
17 #include <net/net_namespace.h>
18 #include <net/page_pool.h>
19 #include <linux/error-injection.h>
20 #include <linux/smp.h>
21 #include <linux/sock_diag.h>
22 #include <net/xdp.h>
23
24 #define CREATE_TRACE_POINTS
25 #include <trace/events/bpf_test_run.h>
26
27 struct bpf_test_timer {
28         enum { NO_PREEMPT, NO_MIGRATE } mode;
29         u32 i;
30         u64 time_start, time_spent;
31 };
32
33 static void bpf_test_timer_enter(struct bpf_test_timer *t)
34         __acquires(rcu)
35 {
36         rcu_read_lock();
37         if (t->mode == NO_PREEMPT)
38                 preempt_disable();
39         else
40                 migrate_disable();
41
42         t->time_start = ktime_get_ns();
43 }
44
45 static void bpf_test_timer_leave(struct bpf_test_timer *t)
46         __releases(rcu)
47 {
48         t->time_start = 0;
49
50         if (t->mode == NO_PREEMPT)
51                 preempt_enable();
52         else
53                 migrate_enable();
54         rcu_read_unlock();
55 }
56
57 static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations,
58                                     u32 repeat, int *err, u32 *duration)
59         __must_hold(rcu)
60 {
61         t->i += iterations;
62         if (t->i >= repeat) {
63                 /* We're done. */
64                 t->time_spent += ktime_get_ns() - t->time_start;
65                 do_div(t->time_spent, t->i);
66                 *duration = t->time_spent > U32_MAX ? U32_MAX : (u32)t->time_spent;
67                 *err = 0;
68                 goto reset;
69         }
70
71         if (signal_pending(current)) {
72                 /* During iteration: we've been cancelled, abort. */
73                 *err = -EINTR;
74                 goto reset;
75         }
76
77         if (need_resched()) {
78                 /* During iteration: we need to reschedule between runs. */
79                 t->time_spent += ktime_get_ns() - t->time_start;
80                 bpf_test_timer_leave(t);
81                 cond_resched();
82                 bpf_test_timer_enter(t);
83         }
84
85         /* Do another round. */
86         return true;
87
88 reset:
89         t->i = 0;
90         return false;
91 }
92
93 /* We put this struct at the head of each page with a context and frame
94  * initialised when the page is allocated, so we don't have to do this on each
95  * repetition of the test run.
96  */
97 struct xdp_page_head {
98         struct xdp_buff orig_ctx;
99         struct xdp_buff ctx;
100         union {
101                 /* ::data_hard_start starts here */
102                 DECLARE_FLEX_ARRAY(struct xdp_frame, frame);
103                 DECLARE_FLEX_ARRAY(u8, data);
104         };
105 };
106
107 struct xdp_test_data {
108         struct xdp_buff *orig_ctx;
109         struct xdp_rxq_info rxq;
110         struct net_device *dev;
111         struct page_pool *pp;
112         struct xdp_frame **frames;
113         struct sk_buff **skbs;
114         struct xdp_mem_info mem;
115         u32 batch_size;
116         u32 frame_cnt;
117 };
118
119 #define TEST_XDP_FRAME_SIZE (PAGE_SIZE - sizeof(struct xdp_page_head))
120 #define TEST_XDP_MAX_BATCH 256
121
122 #if BITS_PER_LONG == 64 && PAGE_SIZE == SZ_4K
123 /* tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c:%MAX_PKT_SIZE
124  * must be updated accordingly when any of these changes, otherwise BPF
125  * selftests will fail.
126  */
127 #ifdef __s390x__
128 #define TEST_MAX_PKT_SIZE 3216
129 #else
130 #define TEST_MAX_PKT_SIZE 3408
131 #endif
132 static_assert(SKB_WITH_OVERHEAD(TEST_XDP_FRAME_SIZE - XDP_PACKET_HEADROOM) ==
133               TEST_MAX_PKT_SIZE);
134 #endif
135
136 static void xdp_test_run_init_page(struct page *page, void *arg)
137 {
138         struct xdp_page_head *head = phys_to_virt(page_to_phys(page));
139         struct xdp_buff *new_ctx, *orig_ctx;
140         u32 headroom = XDP_PACKET_HEADROOM;
141         struct xdp_test_data *xdp = arg;
142         size_t frm_len, meta_len;
143         struct xdp_frame *frm;
144         void *data;
145
146         orig_ctx = xdp->orig_ctx;
147         frm_len = orig_ctx->data_end - orig_ctx->data_meta;
148         meta_len = orig_ctx->data - orig_ctx->data_meta;
149         headroom -= meta_len;
150
151         new_ctx = &head->ctx;
152         frm = head->frame;
153         data = head->data;
154         memcpy(data + headroom, orig_ctx->data_meta, frm_len);
155
156         xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq);
157         xdp_prepare_buff(new_ctx, data, headroom, frm_len, true);
158         new_ctx->data = new_ctx->data_meta + meta_len;
159
160         xdp_update_frame_from_buff(new_ctx, frm);
161         frm->mem = new_ctx->rxq->mem;
162
163         memcpy(&head->orig_ctx, new_ctx, sizeof(head->orig_ctx));
164 }
165
166 static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_ctx)
167 {
168         struct page_pool *pp;
169         int err = -ENOMEM;
170         struct page_pool_params pp_params = {
171                 .order = 0,
172                 .flags = 0,
173                 .pool_size = xdp->batch_size,
174                 .nid = NUMA_NO_NODE,
175                 .init_callback = xdp_test_run_init_page,
176                 .init_arg = xdp,
177         };
178
179         xdp->frames = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL);
180         if (!xdp->frames)
181                 return -ENOMEM;
182
183         xdp->skbs = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL);
184         if (!xdp->skbs)
185                 goto err_skbs;
186
187         pp = page_pool_create(&pp_params);
188         if (IS_ERR(pp)) {
189                 err = PTR_ERR(pp);
190                 goto err_pp;
191         }
192
193         /* will copy 'mem.id' into pp->xdp_mem_id */
194         err = xdp_reg_mem_model(&xdp->mem, MEM_TYPE_PAGE_POOL, pp);
195         if (err)
196                 goto err_mmodel;
197
198         xdp->pp = pp;
199
200         /* We create a 'fake' RXQ referencing the original dev, but with an
201          * xdp_mem_info pointing to our page_pool
202          */
203         xdp_rxq_info_reg(&xdp->rxq, orig_ctx->rxq->dev, 0, 0);
204         xdp->rxq.mem.type = MEM_TYPE_PAGE_POOL;
205         xdp->rxq.mem.id = pp->xdp_mem_id;
206         xdp->dev = orig_ctx->rxq->dev;
207         xdp->orig_ctx = orig_ctx;
208
209         return 0;
210
211 err_mmodel:
212         page_pool_destroy(pp);
213 err_pp:
214         kvfree(xdp->skbs);
215 err_skbs:
216         kvfree(xdp->frames);
217         return err;
218 }
219
220 static void xdp_test_run_teardown(struct xdp_test_data *xdp)
221 {
222         xdp_unreg_mem_model(&xdp->mem);
223         page_pool_destroy(xdp->pp);
224         kfree(xdp->frames);
225         kfree(xdp->skbs);
226 }
227
228 static bool ctx_was_changed(struct xdp_page_head *head)
229 {
230         return head->orig_ctx.data != head->ctx.data ||
231                 head->orig_ctx.data_meta != head->ctx.data_meta ||
232                 head->orig_ctx.data_end != head->ctx.data_end;
233 }
234
235 static void reset_ctx(struct xdp_page_head *head)
236 {
237         if (likely(!ctx_was_changed(head)))
238                 return;
239
240         head->ctx.data = head->orig_ctx.data;
241         head->ctx.data_meta = head->orig_ctx.data_meta;
242         head->ctx.data_end = head->orig_ctx.data_end;
243         xdp_update_frame_from_buff(&head->ctx, head->frame);
244 }
245
246 static int xdp_recv_frames(struct xdp_frame **frames, int nframes,
247                            struct sk_buff **skbs,
248                            struct net_device *dev)
249 {
250         gfp_t gfp = __GFP_ZERO | GFP_ATOMIC;
251         int i, n;
252         LIST_HEAD(list);
253
254         n = kmem_cache_alloc_bulk(skbuff_cache, gfp, nframes, (void **)skbs);
255         if (unlikely(n == 0)) {
256                 for (i = 0; i < nframes; i++)
257                         xdp_return_frame(frames[i]);
258                 return -ENOMEM;
259         }
260
261         for (i = 0; i < nframes; i++) {
262                 struct xdp_frame *xdpf = frames[i];
263                 struct sk_buff *skb = skbs[i];
264
265                 skb = __xdp_build_skb_from_frame(xdpf, skb, dev);
266                 if (!skb) {
267                         xdp_return_frame(xdpf);
268                         continue;
269                 }
270
271                 list_add_tail(&skb->list, &list);
272         }
273         netif_receive_skb_list(&list);
274
275         return 0;
276 }
277
278 static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog,
279                               u32 repeat)
280 {
281         struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
282         int err = 0, act, ret, i, nframes = 0, batch_sz;
283         struct xdp_frame **frames = xdp->frames;
284         struct xdp_page_head *head;
285         struct xdp_frame *frm;
286         bool redirect = false;
287         struct xdp_buff *ctx;
288         struct page *page;
289
290         batch_sz = min_t(u32, repeat, xdp->batch_size);
291
292         local_bh_disable();
293         xdp_set_return_frame_no_direct();
294
295         for (i = 0; i < batch_sz; i++) {
296                 page = page_pool_dev_alloc_pages(xdp->pp);
297                 if (!page) {
298                         err = -ENOMEM;
299                         goto out;
300                 }
301
302                 head = phys_to_virt(page_to_phys(page));
303                 reset_ctx(head);
304                 ctx = &head->ctx;
305                 frm = head->frame;
306                 xdp->frame_cnt++;
307
308                 act = bpf_prog_run_xdp(prog, ctx);
309
310                 /* if program changed pkt bounds we need to update the xdp_frame */
311                 if (unlikely(ctx_was_changed(head))) {
312                         ret = xdp_update_frame_from_buff(ctx, frm);
313                         if (ret) {
314                                 xdp_return_buff(ctx);
315                                 continue;
316                         }
317                 }
318
319                 switch (act) {
320                 case XDP_TX:
321                         /* we can't do a real XDP_TX since we're not in the
322                          * driver, so turn it into a REDIRECT back to the same
323                          * index
324                          */
325                         ri->tgt_index = xdp->dev->ifindex;
326                         ri->map_id = INT_MAX;
327                         ri->map_type = BPF_MAP_TYPE_UNSPEC;
328                         fallthrough;
329                 case XDP_REDIRECT:
330                         redirect = true;
331                         ret = xdp_do_redirect_frame(xdp->dev, ctx, frm, prog);
332                         if (ret)
333                                 xdp_return_buff(ctx);
334                         break;
335                 case XDP_PASS:
336                         frames[nframes++] = frm;
337                         break;
338                 default:
339                         bpf_warn_invalid_xdp_action(NULL, prog, act);
340                         fallthrough;
341                 case XDP_DROP:
342                         xdp_return_buff(ctx);
343                         break;
344                 }
345         }
346
347 out:
348         if (redirect)
349                 xdp_do_flush();
350         if (nframes) {
351                 ret = xdp_recv_frames(frames, nframes, xdp->skbs, xdp->dev);
352                 if (ret)
353                         err = ret;
354         }
355
356         xdp_clear_return_frame_no_direct();
357         local_bh_enable();
358         return err;
359 }
360
361 static int bpf_test_run_xdp_live(struct bpf_prog *prog, struct xdp_buff *ctx,
362                                  u32 repeat, u32 batch_size, u32 *time)
363
364 {
365         struct xdp_test_data xdp = { .batch_size = batch_size };
366         struct bpf_test_timer t = { .mode = NO_MIGRATE };
367         int ret;
368
369         if (!repeat)
370                 repeat = 1;
371
372         ret = xdp_test_run_setup(&xdp, ctx);
373         if (ret)
374                 return ret;
375
376         bpf_test_timer_enter(&t);
377         do {
378                 xdp.frame_cnt = 0;
379                 ret = xdp_test_run_batch(&xdp, prog, repeat - t.i);
380                 if (unlikely(ret < 0))
381                         break;
382         } while (bpf_test_timer_continue(&t, xdp.frame_cnt, repeat, &ret, time));
383         bpf_test_timer_leave(&t);
384
385         xdp_test_run_teardown(&xdp);
386         return ret;
387 }
388
389 static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
390                         u32 *retval, u32 *time, bool xdp)
391 {
392         struct bpf_prog_array_item item = {.prog = prog};
393         struct bpf_run_ctx *old_ctx;
394         struct bpf_cg_run_ctx run_ctx;
395         struct bpf_test_timer t = { NO_MIGRATE };
396         enum bpf_cgroup_storage_type stype;
397         int ret;
398
399         for_each_cgroup_storage_type(stype) {
400                 item.cgroup_storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
401                 if (IS_ERR(item.cgroup_storage[stype])) {
402                         item.cgroup_storage[stype] = NULL;
403                         for_each_cgroup_storage_type(stype)
404                                 bpf_cgroup_storage_free(item.cgroup_storage[stype]);
405                         return -ENOMEM;
406                 }
407         }
408
409         if (!repeat)
410                 repeat = 1;
411
412         bpf_test_timer_enter(&t);
413         old_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
414         do {
415                 run_ctx.prog_item = &item;
416                 local_bh_disable();
417                 if (xdp)
418                         *retval = bpf_prog_run_xdp(prog, ctx);
419                 else
420                         *retval = bpf_prog_run(prog, ctx);
421                 local_bh_enable();
422         } while (bpf_test_timer_continue(&t, 1, repeat, &ret, time));
423         bpf_reset_run_ctx(old_ctx);
424         bpf_test_timer_leave(&t);
425
426         for_each_cgroup_storage_type(stype)
427                 bpf_cgroup_storage_free(item.cgroup_storage[stype]);
428
429         return ret;
430 }
431
432 static int bpf_test_finish(const union bpf_attr *kattr,
433                            union bpf_attr __user *uattr, const void *data,
434                            struct skb_shared_info *sinfo, u32 size,
435                            u32 retval, u32 duration)
436 {
437         void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
438         int err = -EFAULT;
439         u32 copy_size = size;
440
441         /* Clamp copy if the user has provided a size hint, but copy the full
442          * buffer if not to retain old behaviour.
443          */
444         if (kattr->test.data_size_out &&
445             copy_size > kattr->test.data_size_out) {
446                 copy_size = kattr->test.data_size_out;
447                 err = -ENOSPC;
448         }
449
450         if (data_out) {
451                 int len = sinfo ? copy_size - sinfo->xdp_frags_size : copy_size;
452
453                 if (len < 0) {
454                         err = -ENOSPC;
455                         goto out;
456                 }
457
458                 if (copy_to_user(data_out, data, len))
459                         goto out;
460
461                 if (sinfo) {
462                         int i, offset = len;
463                         u32 data_len;
464
465                         for (i = 0; i < sinfo->nr_frags; i++) {
466                                 skb_frag_t *frag = &sinfo->frags[i];
467
468                                 if (offset >= copy_size) {
469                                         err = -ENOSPC;
470                                         break;
471                                 }
472
473                                 data_len = min_t(u32, copy_size - offset,
474                                                  skb_frag_size(frag));
475
476                                 if (copy_to_user(data_out + offset,
477                                                  skb_frag_address(frag),
478                                                  data_len))
479                                         goto out;
480
481                                 offset += data_len;
482                         }
483                 }
484         }
485
486         if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
487                 goto out;
488         if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
489                 goto out;
490         if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
491                 goto out;
492         if (err != -ENOSPC)
493                 err = 0;
494 out:
495         trace_bpf_test_finish(&err);
496         return err;
497 }
498
499 /* Integer types of various sizes and pointer combinations cover variety of
500  * architecture dependent calling conventions. 7+ can be supported in the
501  * future.
502  */
503 __diag_push();
504 __diag_ignore_all("-Wmissing-prototypes",
505                   "Global functions as their definitions will be in vmlinux BTF");
506 __bpf_kfunc int bpf_fentry_test1(int a)
507 {
508         return a + 1;
509 }
510 EXPORT_SYMBOL_GPL(bpf_fentry_test1);
511
512 int noinline bpf_fentry_test2(int a, u64 b)
513 {
514         return a + b;
515 }
516
517 int noinline bpf_fentry_test3(char a, int b, u64 c)
518 {
519         return a + b + c;
520 }
521
522 int noinline bpf_fentry_test4(void *a, char b, int c, u64 d)
523 {
524         return (long)a + b + c + d;
525 }
526
527 int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e)
528 {
529         return a + (long)b + c + d + e;
530 }
531
532 int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f)
533 {
534         return a + (long)b + c + d + (long)e + f;
535 }
536
537 struct bpf_fentry_test_t {
538         struct bpf_fentry_test_t *a;
539 };
540
541 int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg)
542 {
543         return (long)arg;
544 }
545
546 int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg)
547 {
548         return (long)arg->a;
549 }
550
551 __bpf_kfunc int bpf_modify_return_test(int a, int *b)
552 {
553         *b += 1;
554         return a + *b;
555 }
556
557 __bpf_kfunc u64 bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d)
558 {
559         return a + b + c + d;
560 }
561
562 __bpf_kfunc int bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b)
563 {
564         return a + b;
565 }
566
567 __bpf_kfunc struct sock *bpf_kfunc_call_test3(struct sock *sk)
568 {
569         return sk;
570 }
571
572 long noinline bpf_kfunc_call_test4(signed char a, short b, int c, long d)
573 {
574         /* Provoke the compiler to assume that the caller has sign-extended a,
575          * b and c on platforms where this is required (e.g. s390x).
576          */
577         return (long)a + (long)b + (long)c + d;
578 }
579
580 struct prog_test_member1 {
581         int a;
582 };
583
584 struct prog_test_member {
585         struct prog_test_member1 m;
586         int c;
587 };
588
589 struct prog_test_ref_kfunc {
590         int a;
591         int b;
592         struct prog_test_member memb;
593         struct prog_test_ref_kfunc *next;
594         refcount_t cnt;
595 };
596
597 static struct prog_test_ref_kfunc prog_test_struct = {
598         .a = 42,
599         .b = 108,
600         .next = &prog_test_struct,
601         .cnt = REFCOUNT_INIT(1),
602 };
603
604 __bpf_kfunc struct prog_test_ref_kfunc *
605 bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr)
606 {
607         refcount_inc(&prog_test_struct.cnt);
608         return &prog_test_struct;
609 }
610
611 __bpf_kfunc struct prog_test_member *
612 bpf_kfunc_call_memb_acquire(void)
613 {
614         WARN_ON_ONCE(1);
615         return NULL;
616 }
617
618 __bpf_kfunc void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p)
619 {
620         if (!p)
621                 return;
622
623         refcount_dec(&p->cnt);
624 }
625
626 __bpf_kfunc void bpf_kfunc_call_memb_release(struct prog_test_member *p)
627 {
628 }
629
630 __bpf_kfunc void bpf_kfunc_call_memb1_release(struct prog_test_member1 *p)
631 {
632         WARN_ON_ONCE(1);
633 }
634
635 static int *__bpf_kfunc_call_test_get_mem(struct prog_test_ref_kfunc *p, const int size)
636 {
637         if (size > 2 * sizeof(int))
638                 return NULL;
639
640         return (int *)p;
641 }
642
643 __bpf_kfunc int *bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc *p,
644                                                   const int rdwr_buf_size)
645 {
646         return __bpf_kfunc_call_test_get_mem(p, rdwr_buf_size);
647 }
648
649 __bpf_kfunc int *bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc *p,
650                                                     const int rdonly_buf_size)
651 {
652         return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size);
653 }
654
655 /* the next 2 ones can't be really used for testing expect to ensure
656  * that the verifier rejects the call.
657  * Acquire functions must return struct pointers, so these ones are
658  * failing.
659  */
660 __bpf_kfunc int *bpf_kfunc_call_test_acq_rdonly_mem(struct prog_test_ref_kfunc *p,
661                                                     const int rdonly_buf_size)
662 {
663         return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size);
664 }
665
666 __bpf_kfunc void bpf_kfunc_call_int_mem_release(int *p)
667 {
668 }
669
670 __bpf_kfunc struct prog_test_ref_kfunc *
671 bpf_kfunc_call_test_kptr_get(struct prog_test_ref_kfunc **pp, int a, int b)
672 {
673         struct prog_test_ref_kfunc *p = READ_ONCE(*pp);
674
675         if (!p)
676                 return NULL;
677         refcount_inc(&p->cnt);
678         return p;
679 }
680
681 struct prog_test_pass1 {
682         int x0;
683         struct {
684                 int x1;
685                 struct {
686                         int x2;
687                         struct {
688                                 int x3;
689                         };
690                 };
691         };
692 };
693
694 struct prog_test_pass2 {
695         int len;
696         short arr1[4];
697         struct {
698                 char arr2[4];
699                 unsigned long arr3[8];
700         } x;
701 };
702
703 struct prog_test_fail1 {
704         void *p;
705         int x;
706 };
707
708 struct prog_test_fail2 {
709         int x8;
710         struct prog_test_pass1 x;
711 };
712
713 struct prog_test_fail3 {
714         int len;
715         char arr1[2];
716         char arr2[];
717 };
718
719 __bpf_kfunc void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb)
720 {
721 }
722
723 __bpf_kfunc void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p)
724 {
725 }
726
727 __bpf_kfunc void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p)
728 {
729 }
730
731 __bpf_kfunc void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p)
732 {
733 }
734
735 __bpf_kfunc void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p)
736 {
737 }
738
739 __bpf_kfunc void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p)
740 {
741 }
742
743 __bpf_kfunc void bpf_kfunc_call_test_mem_len_pass1(void *mem, int mem__sz)
744 {
745 }
746
747 __bpf_kfunc void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len)
748 {
749 }
750
751 __bpf_kfunc void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len)
752 {
753 }
754
755 __bpf_kfunc void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p)
756 {
757 }
758
759 __bpf_kfunc void bpf_kfunc_call_test_destructive(void)
760 {
761 }
762
763 __bpf_kfunc static u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unused)
764 {
765         return arg;
766 }
767
768 __diag_pop();
769
770 BTF_SET8_START(bpf_test_modify_return_ids)
771 BTF_ID_FLAGS(func, bpf_modify_return_test)
772 BTF_ID_FLAGS(func, bpf_fentry_test1, KF_SLEEPABLE)
773 BTF_SET8_END(bpf_test_modify_return_ids)
774
775 static const struct btf_kfunc_id_set bpf_test_modify_return_set = {
776         .owner = THIS_MODULE,
777         .set   = &bpf_test_modify_return_ids,
778 };
779
780 BTF_SET8_START(test_sk_check_kfunc_ids)
781 BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
782 BTF_ID_FLAGS(func, bpf_kfunc_call_test2)
783 BTF_ID_FLAGS(func, bpf_kfunc_call_test3)
784 BTF_ID_FLAGS(func, bpf_kfunc_call_test4)
785 BTF_ID_FLAGS(func, bpf_kfunc_call_test_acquire, KF_ACQUIRE | KF_RET_NULL)
786 BTF_ID_FLAGS(func, bpf_kfunc_call_memb_acquire, KF_ACQUIRE | KF_RET_NULL)
787 BTF_ID_FLAGS(func, bpf_kfunc_call_test_release, KF_RELEASE)
788 BTF_ID_FLAGS(func, bpf_kfunc_call_memb_release, KF_RELEASE)
789 BTF_ID_FLAGS(func, bpf_kfunc_call_memb1_release, KF_RELEASE)
790 BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdwr_mem, KF_RET_NULL)
791 BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdonly_mem, KF_RET_NULL)
792 BTF_ID_FLAGS(func, bpf_kfunc_call_test_acq_rdonly_mem, KF_ACQUIRE | KF_RET_NULL)
793 BTF_ID_FLAGS(func, bpf_kfunc_call_int_mem_release, KF_RELEASE)
794 BTF_ID_FLAGS(func, bpf_kfunc_call_test_kptr_get, KF_ACQUIRE | KF_RET_NULL | KF_KPTR_GET)
795 BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass_ctx)
796 BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass1)
797 BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2)
798 BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail1)
799 BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail2)
800 BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail3)
801 BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1)
802 BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1)
803 BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2)
804 BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS)
805 BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE)
806 BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg)
807 BTF_SET8_END(test_sk_check_kfunc_ids)
808
809 static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,
810                            u32 size, u32 headroom, u32 tailroom)
811 {
812         void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
813         void *data;
814
815         if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom)
816                 return ERR_PTR(-EINVAL);
817
818         if (user_size > size)
819                 return ERR_PTR(-EMSGSIZE);
820
821         size = SKB_DATA_ALIGN(size);
822         data = kzalloc(size + headroom + tailroom, GFP_USER);
823         if (!data)
824                 return ERR_PTR(-ENOMEM);
825
826         if (copy_from_user(data + headroom, data_in, user_size)) {
827                 kfree(data);
828                 return ERR_PTR(-EFAULT);
829         }
830
831         return data;
832 }
833
834 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
835                               const union bpf_attr *kattr,
836                               union bpf_attr __user *uattr)
837 {
838         struct bpf_fentry_test_t arg = {};
839         u16 side_effect = 0, ret = 0;
840         int b = 2, err = -EFAULT;
841         u32 retval = 0;
842
843         if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
844                 return -EINVAL;
845
846         switch (prog->expected_attach_type) {
847         case BPF_TRACE_FENTRY:
848         case BPF_TRACE_FEXIT:
849                 if (bpf_fentry_test1(1) != 2 ||
850                     bpf_fentry_test2(2, 3) != 5 ||
851                     bpf_fentry_test3(4, 5, 6) != 15 ||
852                     bpf_fentry_test4((void *)7, 8, 9, 10) != 34 ||
853                     bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 ||
854                     bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 ||
855                     bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 ||
856                     bpf_fentry_test8(&arg) != 0)
857                         goto out;
858                 break;
859         case BPF_MODIFY_RETURN:
860                 ret = bpf_modify_return_test(1, &b);
861                 if (b != 2)
862                         side_effect = 1;
863                 break;
864         default:
865                 goto out;
866         }
867
868         retval = ((u32)side_effect << 16) | ret;
869         if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
870                 goto out;
871
872         err = 0;
873 out:
874         trace_bpf_test_finish(&err);
875         return err;
876 }
877
878 struct bpf_raw_tp_test_run_info {
879         struct bpf_prog *prog;
880         void *ctx;
881         u32 retval;
882 };
883
884 static void
885 __bpf_prog_test_run_raw_tp(void *data)
886 {
887         struct bpf_raw_tp_test_run_info *info = data;
888
889         rcu_read_lock();
890         info->retval = bpf_prog_run(info->prog, info->ctx);
891         rcu_read_unlock();
892 }
893
894 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
895                              const union bpf_attr *kattr,
896                              union bpf_attr __user *uattr)
897 {
898         void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
899         __u32 ctx_size_in = kattr->test.ctx_size_in;
900         struct bpf_raw_tp_test_run_info info;
901         int cpu = kattr->test.cpu, err = 0;
902         int current_cpu;
903
904         /* doesn't support data_in/out, ctx_out, duration, or repeat */
905         if (kattr->test.data_in || kattr->test.data_out ||
906             kattr->test.ctx_out || kattr->test.duration ||
907             kattr->test.repeat || kattr->test.batch_size)
908                 return -EINVAL;
909
910         if (ctx_size_in < prog->aux->max_ctx_offset ||
911             ctx_size_in > MAX_BPF_FUNC_ARGS * sizeof(u64))
912                 return -EINVAL;
913
914         if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0)
915                 return -EINVAL;
916
917         if (ctx_size_in) {
918                 info.ctx = memdup_user(ctx_in, ctx_size_in);
919                 if (IS_ERR(info.ctx))
920                         return PTR_ERR(info.ctx);
921         } else {
922                 info.ctx = NULL;
923         }
924
925         info.prog = prog;
926
927         current_cpu = get_cpu();
928         if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 ||
929             cpu == current_cpu) {
930                 __bpf_prog_test_run_raw_tp(&info);
931         } else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {
932                 /* smp_call_function_single() also checks cpu_online()
933                  * after csd_lock(). However, since cpu is from user
934                  * space, let's do an extra quick check to filter out
935                  * invalid value before smp_call_function_single().
936                  */
937                 err = -ENXIO;
938         } else {
939                 err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp,
940                                                &info, 1);
941         }
942         put_cpu();
943
944         if (!err &&
945             copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32)))
946                 err = -EFAULT;
947
948         kfree(info.ctx);
949         return err;
950 }
951
952 static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size)
953 {
954         void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in);
955         void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
956         u32 size = kattr->test.ctx_size_in;
957         void *data;
958         int err;
959
960         if (!data_in && !data_out)
961                 return NULL;
962
963         data = kzalloc(max_size, GFP_USER);
964         if (!data)
965                 return ERR_PTR(-ENOMEM);
966
967         if (data_in) {
968                 err = bpf_check_uarg_tail_zero(USER_BPFPTR(data_in), max_size, size);
969                 if (err) {
970                         kfree(data);
971                         return ERR_PTR(err);
972                 }
973
974                 size = min_t(u32, max_size, size);
975                 if (copy_from_user(data, data_in, size)) {
976                         kfree(data);
977                         return ERR_PTR(-EFAULT);
978                 }
979         }
980         return data;
981 }
982
983 static int bpf_ctx_finish(const union bpf_attr *kattr,
984                           union bpf_attr __user *uattr, const void *data,
985                           u32 size)
986 {
987         void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
988         int err = -EFAULT;
989         u32 copy_size = size;
990
991         if (!data || !data_out)
992                 return 0;
993
994         if (copy_size > kattr->test.ctx_size_out) {
995                 copy_size = kattr->test.ctx_size_out;
996                 err = -ENOSPC;
997         }
998
999         if (copy_to_user(data_out, data, copy_size))
1000                 goto out;
1001         if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size)))
1002                 goto out;
1003         if (err != -ENOSPC)
1004                 err = 0;
1005 out:
1006         return err;
1007 }
1008
1009 /**
1010  * range_is_zero - test whether buffer is initialized
1011  * @buf: buffer to check
1012  * @from: check from this position
1013  * @to: check up until (excluding) this position
1014  *
1015  * This function returns true if the there is a non-zero byte
1016  * in the buf in the range [from,to).
1017  */
1018 static inline bool range_is_zero(void *buf, size_t from, size_t to)
1019 {
1020         return !memchr_inv((u8 *)buf + from, 0, to - from);
1021 }
1022
1023 static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
1024 {
1025         struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
1026
1027         if (!__skb)
1028                 return 0;
1029
1030         /* make sure the fields we don't use are zeroed */
1031         if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark)))
1032                 return -EINVAL;
1033
1034         /* mark is allowed */
1035
1036         if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark),
1037                            offsetof(struct __sk_buff, priority)))
1038                 return -EINVAL;
1039
1040         /* priority is allowed */
1041         /* ingress_ifindex is allowed */
1042         /* ifindex is allowed */
1043
1044         if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex),
1045                            offsetof(struct __sk_buff, cb)))
1046                 return -EINVAL;
1047
1048         /* cb is allowed */
1049
1050         if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb),
1051                            offsetof(struct __sk_buff, tstamp)))
1052                 return -EINVAL;
1053
1054         /* tstamp is allowed */
1055         /* wire_len is allowed */
1056         /* gso_segs is allowed */
1057
1058         if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs),
1059                            offsetof(struct __sk_buff, gso_size)))
1060                 return -EINVAL;
1061
1062         /* gso_size is allowed */
1063
1064         if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size),
1065                            offsetof(struct __sk_buff, hwtstamp)))
1066                 return -EINVAL;
1067
1068         /* hwtstamp is allowed */
1069
1070         if (!range_is_zero(__skb, offsetofend(struct __sk_buff, hwtstamp),
1071                            sizeof(struct __sk_buff)))
1072                 return -EINVAL;
1073
1074         skb->mark = __skb->mark;
1075         skb->priority = __skb->priority;
1076         skb->skb_iif = __skb->ingress_ifindex;
1077         skb->tstamp = __skb->tstamp;
1078         memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN);
1079
1080         if (__skb->wire_len == 0) {
1081                 cb->pkt_len = skb->len;
1082         } else {
1083                 if (__skb->wire_len < skb->len ||
1084                     __skb->wire_len > GSO_LEGACY_MAX_SIZE)
1085                         return -EINVAL;
1086                 cb->pkt_len = __skb->wire_len;
1087         }
1088
1089         if (__skb->gso_segs > GSO_MAX_SEGS)
1090                 return -EINVAL;
1091         skb_shinfo(skb)->gso_segs = __skb->gso_segs;
1092         skb_shinfo(skb)->gso_size = __skb->gso_size;
1093         skb_shinfo(skb)->hwtstamps.hwtstamp = __skb->hwtstamp;
1094
1095         return 0;
1096 }
1097
1098 static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb)
1099 {
1100         struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
1101
1102         if (!__skb)
1103                 return;
1104
1105         __skb->mark = skb->mark;
1106         __skb->priority = skb->priority;
1107         __skb->ingress_ifindex = skb->skb_iif;
1108         __skb->ifindex = skb->dev->ifindex;
1109         __skb->tstamp = skb->tstamp;
1110         memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN);
1111         __skb->wire_len = cb->pkt_len;
1112         __skb->gso_segs = skb_shinfo(skb)->gso_segs;
1113         __skb->hwtstamp = skb_shinfo(skb)->hwtstamps.hwtstamp;
1114 }
1115
1116 static struct proto bpf_dummy_proto = {
1117         .name   = "bpf_dummy",
1118         .owner  = THIS_MODULE,
1119         .obj_size = sizeof(struct sock),
1120 };
1121
1122 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1123                           union bpf_attr __user *uattr)
1124 {
1125         bool is_l2 = false, is_direct_pkt_access = false;
1126         struct net *net = current->nsproxy->net_ns;
1127         struct net_device *dev = net->loopback_dev;
1128         u32 size = kattr->test.data_size_in;
1129         u32 repeat = kattr->test.repeat;
1130         struct __sk_buff *ctx = NULL;
1131         u32 retval, duration;
1132         int hh_len = ETH_HLEN;
1133         struct sk_buff *skb;
1134         struct sock *sk;
1135         void *data;
1136         int ret;
1137
1138         if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
1139                 return -EINVAL;
1140
1141         data = bpf_test_init(kattr, kattr->test.data_size_in,
1142                              size, NET_SKB_PAD + NET_IP_ALIGN,
1143                              SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
1144         if (IS_ERR(data))
1145                 return PTR_ERR(data);
1146
1147         ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
1148         if (IS_ERR(ctx)) {
1149                 kfree(data);
1150                 return PTR_ERR(ctx);
1151         }
1152
1153         switch (prog->type) {
1154         case BPF_PROG_TYPE_SCHED_CLS:
1155         case BPF_PROG_TYPE_SCHED_ACT:
1156                 is_l2 = true;
1157                 fallthrough;
1158         case BPF_PROG_TYPE_LWT_IN:
1159         case BPF_PROG_TYPE_LWT_OUT:
1160         case BPF_PROG_TYPE_LWT_XMIT:
1161                 is_direct_pkt_access = true;
1162                 break;
1163         default:
1164                 break;
1165         }
1166
1167         sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
1168         if (!sk) {
1169                 kfree(data);
1170                 kfree(ctx);
1171                 return -ENOMEM;
1172         }
1173         sock_init_data(NULL, sk);
1174
1175         skb = slab_build_skb(data);
1176         if (!skb) {
1177                 kfree(data);
1178                 kfree(ctx);
1179                 sk_free(sk);
1180                 return -ENOMEM;
1181         }
1182         skb->sk = sk;
1183
1184         skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1185         __skb_put(skb, size);
1186         if (ctx && ctx->ifindex > 1) {
1187                 dev = dev_get_by_index(net, ctx->ifindex);
1188                 if (!dev) {
1189                         ret = -ENODEV;
1190                         goto out;
1191                 }
1192         }
1193         skb->protocol = eth_type_trans(skb, dev);
1194         skb_reset_network_header(skb);
1195
1196         switch (skb->protocol) {
1197         case htons(ETH_P_IP):
1198                 sk->sk_family = AF_INET;
1199                 if (sizeof(struct iphdr) <= skb_headlen(skb)) {
1200                         sk->sk_rcv_saddr = ip_hdr(skb)->saddr;
1201                         sk->sk_daddr = ip_hdr(skb)->daddr;
1202                 }
1203                 break;
1204 #if IS_ENABLED(CONFIG_IPV6)
1205         case htons(ETH_P_IPV6):
1206                 sk->sk_family = AF_INET6;
1207                 if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) {
1208                         sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr;
1209                         sk->sk_v6_daddr = ipv6_hdr(skb)->daddr;
1210                 }
1211                 break;
1212 #endif
1213         default:
1214                 break;
1215         }
1216
1217         if (is_l2)
1218                 __skb_push(skb, hh_len);
1219         if (is_direct_pkt_access)
1220                 bpf_compute_data_pointers(skb);
1221         ret = convert___skb_to_skb(skb, ctx);
1222         if (ret)
1223                 goto out;
1224         ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false);
1225         if (ret)
1226                 goto out;
1227         if (!is_l2) {
1228                 if (skb_headroom(skb) < hh_len) {
1229                         int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
1230
1231                         if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
1232                                 ret = -ENOMEM;
1233                                 goto out;
1234                         }
1235                 }
1236                 memset(__skb_push(skb, hh_len), 0, hh_len);
1237         }
1238         convert_skb_to___skb(skb, ctx);
1239
1240         size = skb->len;
1241         /* bpf program can never convert linear skb to non-linear */
1242         if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
1243                 size = skb_headlen(skb);
1244         ret = bpf_test_finish(kattr, uattr, skb->data, NULL, size, retval,
1245                               duration);
1246         if (!ret)
1247                 ret = bpf_ctx_finish(kattr, uattr, ctx,
1248                                      sizeof(struct __sk_buff));
1249 out:
1250         if (dev && dev != net->loopback_dev)
1251                 dev_put(dev);
1252         kfree_skb(skb);
1253         sk_free(sk);
1254         kfree(ctx);
1255         return ret;
1256 }
1257
1258 static int xdp_convert_md_to_buff(struct xdp_md *xdp_md, struct xdp_buff *xdp)
1259 {
1260         unsigned int ingress_ifindex, rx_queue_index;
1261         struct netdev_rx_queue *rxqueue;
1262         struct net_device *device;
1263
1264         if (!xdp_md)
1265                 return 0;
1266
1267         if (xdp_md->egress_ifindex != 0)
1268                 return -EINVAL;
1269
1270         ingress_ifindex = xdp_md->ingress_ifindex;
1271         rx_queue_index = xdp_md->rx_queue_index;
1272
1273         if (!ingress_ifindex && rx_queue_index)
1274                 return -EINVAL;
1275
1276         if (ingress_ifindex) {
1277                 device = dev_get_by_index(current->nsproxy->net_ns,
1278                                           ingress_ifindex);
1279                 if (!device)
1280                         return -ENODEV;
1281
1282                 if (rx_queue_index >= device->real_num_rx_queues)
1283                         goto free_dev;
1284
1285                 rxqueue = __netif_get_rx_queue(device, rx_queue_index);
1286
1287                 if (!xdp_rxq_info_is_reg(&rxqueue->xdp_rxq))
1288                         goto free_dev;
1289
1290                 xdp->rxq = &rxqueue->xdp_rxq;
1291                 /* The device is now tracked in the xdp->rxq for later
1292                  * dev_put()
1293                  */
1294         }
1295
1296         xdp->data = xdp->data_meta + xdp_md->data;
1297         return 0;
1298
1299 free_dev:
1300         dev_put(device);
1301         return -EINVAL;
1302 }
1303
1304 static void xdp_convert_buff_to_md(struct xdp_buff *xdp, struct xdp_md *xdp_md)
1305 {
1306         if (!xdp_md)
1307                 return;
1308
1309         xdp_md->data = xdp->data - xdp->data_meta;
1310         xdp_md->data_end = xdp->data_end - xdp->data_meta;
1311
1312         if (xdp_md->ingress_ifindex)
1313                 dev_put(xdp->rxq->dev);
1314 }
1315
1316 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1317                           union bpf_attr __user *uattr)
1318 {
1319         bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES);
1320         u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1321         u32 batch_size = kattr->test.batch_size;
1322         u32 retval = 0, duration, max_data_sz;
1323         u32 size = kattr->test.data_size_in;
1324         u32 headroom = XDP_PACKET_HEADROOM;
1325         u32 repeat = kattr->test.repeat;
1326         struct netdev_rx_queue *rxqueue;
1327         struct skb_shared_info *sinfo;
1328         struct xdp_buff xdp = {};
1329         int i, ret = -EINVAL;
1330         struct xdp_md *ctx;
1331         void *data;
1332
1333         if (prog->expected_attach_type == BPF_XDP_DEVMAP ||
1334             prog->expected_attach_type == BPF_XDP_CPUMAP)
1335                 return -EINVAL;
1336
1337         if (kattr->test.flags & ~BPF_F_TEST_XDP_LIVE_FRAMES)
1338                 return -EINVAL;
1339
1340         if (bpf_prog_is_dev_bound(prog->aux))
1341                 return -EINVAL;
1342
1343         if (do_live) {
1344                 if (!batch_size)
1345                         batch_size = NAPI_POLL_WEIGHT;
1346                 else if (batch_size > TEST_XDP_MAX_BATCH)
1347                         return -E2BIG;
1348
1349                 headroom += sizeof(struct xdp_page_head);
1350         } else if (batch_size) {
1351                 return -EINVAL;
1352         }
1353
1354         ctx = bpf_ctx_init(kattr, sizeof(struct xdp_md));
1355         if (IS_ERR(ctx))
1356                 return PTR_ERR(ctx);
1357
1358         if (ctx) {
1359                 /* There can't be user provided data before the meta data */
1360                 if (ctx->data_meta || ctx->data_end != size ||
1361                     ctx->data > ctx->data_end ||
1362                     unlikely(xdp_metalen_invalid(ctx->data)) ||
1363                     (do_live && (kattr->test.data_out || kattr->test.ctx_out)))
1364                         goto free_ctx;
1365                 /* Meta data is allocated from the headroom */
1366                 headroom -= ctx->data;
1367         }
1368
1369         max_data_sz = 4096 - headroom - tailroom;
1370         if (size > max_data_sz) {
1371                 /* disallow live data mode for jumbo frames */
1372                 if (do_live)
1373                         goto free_ctx;
1374                 size = max_data_sz;
1375         }
1376
1377         data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom);
1378         if (IS_ERR(data)) {
1379                 ret = PTR_ERR(data);
1380                 goto free_ctx;
1381         }
1382
1383         rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
1384         rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom;
1385         xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq);
1386         xdp_prepare_buff(&xdp, data, headroom, size, true);
1387         sinfo = xdp_get_shared_info_from_buff(&xdp);
1388
1389         ret = xdp_convert_md_to_buff(ctx, &xdp);
1390         if (ret)
1391                 goto free_data;
1392
1393         if (unlikely(kattr->test.data_size_in > size)) {
1394                 void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
1395
1396                 while (size < kattr->test.data_size_in) {
1397                         struct page *page;
1398                         skb_frag_t *frag;
1399                         u32 data_len;
1400
1401                         if (sinfo->nr_frags == MAX_SKB_FRAGS) {
1402                                 ret = -ENOMEM;
1403                                 goto out;
1404                         }
1405
1406                         page = alloc_page(GFP_KERNEL);
1407                         if (!page) {
1408                                 ret = -ENOMEM;
1409                                 goto out;
1410                         }
1411
1412                         frag = &sinfo->frags[sinfo->nr_frags++];
1413                         __skb_frag_set_page(frag, page);
1414
1415                         data_len = min_t(u32, kattr->test.data_size_in - size,
1416                                          PAGE_SIZE);
1417                         skb_frag_size_set(frag, data_len);
1418
1419                         if (copy_from_user(page_address(page), data_in + size,
1420                                            data_len)) {
1421                                 ret = -EFAULT;
1422                                 goto out;
1423                         }
1424                         sinfo->xdp_frags_size += data_len;
1425                         size += data_len;
1426                 }
1427                 xdp_buff_set_frags_flag(&xdp);
1428         }
1429
1430         if (repeat > 1)
1431                 bpf_prog_change_xdp(NULL, prog);
1432
1433         if (do_live)
1434                 ret = bpf_test_run_xdp_live(prog, &xdp, repeat, batch_size, &duration);
1435         else
1436                 ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true);
1437         /* We convert the xdp_buff back to an xdp_md before checking the return
1438          * code so the reference count of any held netdevice will be decremented
1439          * even if the test run failed.
1440          */
1441         xdp_convert_buff_to_md(&xdp, ctx);
1442         if (ret)
1443                 goto out;
1444
1445         size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size;
1446         ret = bpf_test_finish(kattr, uattr, xdp.data_meta, sinfo, size,
1447                               retval, duration);
1448         if (!ret)
1449                 ret = bpf_ctx_finish(kattr, uattr, ctx,
1450                                      sizeof(struct xdp_md));
1451
1452 out:
1453         if (repeat > 1)
1454                 bpf_prog_change_xdp(prog, NULL);
1455 free_data:
1456         for (i = 0; i < sinfo->nr_frags; i++)
1457                 __free_page(skb_frag_page(&sinfo->frags[i]));
1458         kfree(data);
1459 free_ctx:
1460         kfree(ctx);
1461         return ret;
1462 }
1463
1464 static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx)
1465 {
1466         /* make sure the fields we don't use are zeroed */
1467         if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags)))
1468                 return -EINVAL;
1469
1470         /* flags is allowed */
1471
1472         if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags),
1473                            sizeof(struct bpf_flow_keys)))
1474                 return -EINVAL;
1475
1476         return 0;
1477 }
1478
1479 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1480                                      const union bpf_attr *kattr,
1481                                      union bpf_attr __user *uattr)
1482 {
1483         struct bpf_test_timer t = { NO_PREEMPT };
1484         u32 size = kattr->test.data_size_in;
1485         struct bpf_flow_dissector ctx = {};
1486         u32 repeat = kattr->test.repeat;
1487         struct bpf_flow_keys *user_ctx;
1488         struct bpf_flow_keys flow_keys;
1489         const struct ethhdr *eth;
1490         unsigned int flags = 0;
1491         u32 retval, duration;
1492         void *data;
1493         int ret;
1494
1495         if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
1496                 return -EINVAL;
1497
1498         if (size < ETH_HLEN)
1499                 return -EINVAL;
1500
1501         data = bpf_test_init(kattr, kattr->test.data_size_in, size, 0, 0);
1502         if (IS_ERR(data))
1503                 return PTR_ERR(data);
1504
1505         eth = (struct ethhdr *)data;
1506
1507         if (!repeat)
1508                 repeat = 1;
1509
1510         user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys));
1511         if (IS_ERR(user_ctx)) {
1512                 kfree(data);
1513                 return PTR_ERR(user_ctx);
1514         }
1515         if (user_ctx) {
1516                 ret = verify_user_bpf_flow_keys(user_ctx);
1517                 if (ret)
1518                         goto out;
1519                 flags = user_ctx->flags;
1520         }
1521
1522         ctx.flow_keys = &flow_keys;
1523         ctx.data = data;
1524         ctx.data_end = (__u8 *)data + size;
1525
1526         bpf_test_timer_enter(&t);
1527         do {
1528                 retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN,
1529                                           size, flags);
1530         } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration));
1531         bpf_test_timer_leave(&t);
1532
1533         if (ret < 0)
1534                 goto out;
1535
1536         ret = bpf_test_finish(kattr, uattr, &flow_keys, NULL,
1537                               sizeof(flow_keys), retval, duration);
1538         if (!ret)
1539                 ret = bpf_ctx_finish(kattr, uattr, user_ctx,
1540                                      sizeof(struct bpf_flow_keys));
1541
1542 out:
1543         kfree(user_ctx);
1544         kfree(data);
1545         return ret;
1546 }
1547
1548 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr,
1549                                 union bpf_attr __user *uattr)
1550 {
1551         struct bpf_test_timer t = { NO_PREEMPT };
1552         struct bpf_prog_array *progs = NULL;
1553         struct bpf_sk_lookup_kern ctx = {};
1554         u32 repeat = kattr->test.repeat;
1555         struct bpf_sk_lookup *user_ctx;
1556         u32 retval, duration;
1557         int ret = -EINVAL;
1558
1559         if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
1560                 return -EINVAL;
1561
1562         if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out ||
1563             kattr->test.data_size_out)
1564                 return -EINVAL;
1565
1566         if (!repeat)
1567                 repeat = 1;
1568
1569         user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx));
1570         if (IS_ERR(user_ctx))
1571                 return PTR_ERR(user_ctx);
1572
1573         if (!user_ctx)
1574                 return -EINVAL;
1575
1576         if (user_ctx->sk)
1577                 goto out;
1578
1579         if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx)))
1580                 goto out;
1581
1582         if (user_ctx->local_port > U16_MAX) {
1583                 ret = -ERANGE;
1584                 goto out;
1585         }
1586
1587         ctx.family = (u16)user_ctx->family;
1588         ctx.protocol = (u16)user_ctx->protocol;
1589         ctx.dport = (u16)user_ctx->local_port;
1590         ctx.sport = user_ctx->remote_port;
1591
1592         switch (ctx.family) {
1593         case AF_INET:
1594                 ctx.v4.daddr = (__force __be32)user_ctx->local_ip4;
1595                 ctx.v4.saddr = (__force __be32)user_ctx->remote_ip4;
1596                 break;
1597
1598 #if IS_ENABLED(CONFIG_IPV6)
1599         case AF_INET6:
1600                 ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6;
1601                 ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6;
1602                 break;
1603 #endif
1604
1605         default:
1606                 ret = -EAFNOSUPPORT;
1607                 goto out;
1608         }
1609
1610         progs = bpf_prog_array_alloc(1, GFP_KERNEL);
1611         if (!progs) {
1612                 ret = -ENOMEM;
1613                 goto out;
1614         }
1615
1616         progs->items[0].prog = prog;
1617
1618         bpf_test_timer_enter(&t);
1619         do {
1620                 ctx.selected_sk = NULL;
1621                 retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, bpf_prog_run);
1622         } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration));
1623         bpf_test_timer_leave(&t);
1624
1625         if (ret < 0)
1626                 goto out;
1627
1628         user_ctx->cookie = 0;
1629         if (ctx.selected_sk) {
1630                 if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) {
1631                         ret = -EOPNOTSUPP;
1632                         goto out;
1633                 }
1634
1635                 user_ctx->cookie = sock_gen_cookie(ctx.selected_sk);
1636         }
1637
1638         ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration);
1639         if (!ret)
1640                 ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx));
1641
1642 out:
1643         bpf_prog_array_free(progs);
1644         kfree(user_ctx);
1645         return ret;
1646 }
1647
1648 int bpf_prog_test_run_syscall(struct bpf_prog *prog,
1649                               const union bpf_attr *kattr,
1650                               union bpf_attr __user *uattr)
1651 {
1652         void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
1653         __u32 ctx_size_in = kattr->test.ctx_size_in;
1654         void *ctx = NULL;
1655         u32 retval;
1656         int err = 0;
1657
1658         /* doesn't support data_in/out, ctx_out, duration, or repeat or flags */
1659         if (kattr->test.data_in || kattr->test.data_out ||
1660             kattr->test.ctx_out || kattr->test.duration ||
1661             kattr->test.repeat || kattr->test.flags ||
1662             kattr->test.batch_size)
1663                 return -EINVAL;
1664
1665         if (ctx_size_in < prog->aux->max_ctx_offset ||
1666             ctx_size_in > U16_MAX)
1667                 return -EINVAL;
1668
1669         if (ctx_size_in) {
1670                 ctx = memdup_user(ctx_in, ctx_size_in);
1671                 if (IS_ERR(ctx))
1672                         return PTR_ERR(ctx);
1673         }
1674
1675         rcu_read_lock_trace();
1676         retval = bpf_prog_run_pin_on_cpu(prog, ctx);
1677         rcu_read_unlock_trace();
1678
1679         if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) {
1680                 err = -EFAULT;
1681                 goto out;
1682         }
1683         if (ctx_size_in)
1684                 if (copy_to_user(ctx_in, ctx, ctx_size_in))
1685                         err = -EFAULT;
1686 out:
1687         kfree(ctx);
1688         return err;
1689 }
1690
1691 static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = {
1692         .owner = THIS_MODULE,
1693         .set   = &test_sk_check_kfunc_ids,
1694 };
1695
1696 BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids)
1697 BTF_ID(struct, prog_test_ref_kfunc)
1698 BTF_ID(func, bpf_kfunc_call_test_release)
1699 BTF_ID(struct, prog_test_member)
1700 BTF_ID(func, bpf_kfunc_call_memb_release)
1701
1702 static int __init bpf_prog_test_run_init(void)
1703 {
1704         const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = {
1705                 {
1706                   .btf_id       = bpf_prog_test_dtor_kfunc_ids[0],
1707                   .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1]
1708                 },
1709                 {
1710                   .btf_id       = bpf_prog_test_dtor_kfunc_ids[2],
1711                   .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[3],
1712                 },
1713         };
1714         int ret;
1715
1716         ret = register_btf_fmodret_id_set(&bpf_test_modify_return_set);
1717         ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set);
1718         ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_prog_test_kfunc_set);
1719         ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_prog_test_kfunc_set);
1720         return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc,
1721                                                   ARRAY_SIZE(bpf_prog_test_dtor_kfunc),
1722                                                   THIS_MODULE);
1723 }
1724 late_initcall(bpf_prog_test_run_init);