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