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