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