xref: /linux/kernel/bpf/core.c (revision 3ad0876554cafa368f574d4d408468510543e9ff)
1 /*
2  * Linux Socket Filter - Kernel level socket filtering
3  *
4  * Based on the design of the Berkeley Packet Filter. The new
5  * internal format has been designed by PLUMgrid:
6  *
7  *	Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
8  *
9  * Authors:
10  *
11  *	Jay Schulist <jschlst@samba.org>
12  *	Alexei Starovoitov <ast@plumgrid.com>
13  *	Daniel Borkmann <dborkman@redhat.com>
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version
18  * 2 of the License, or (at your option) any later version.
19  *
20  * Andi Kleen - Fix a few bad bugs and races.
21  * Kris Katterjohn - Added many additional checks in bpf_check_classic()
22  */
23 
24 #include <linux/filter.h>
25 #include <linux/skbuff.h>
26 #include <linux/vmalloc.h>
27 #include <linux/random.h>
28 #include <linux/moduleloader.h>
29 #include <linux/bpf.h>
30 #include <linux/frame.h>
31 #include <linux/rbtree_latch.h>
32 #include <linux/kallsyms.h>
33 #include <linux/rcupdate.h>
34 
35 #include <asm/unaligned.h>
36 
37 /* Registers */
38 #define BPF_R0	regs[BPF_REG_0]
39 #define BPF_R1	regs[BPF_REG_1]
40 #define BPF_R2	regs[BPF_REG_2]
41 #define BPF_R3	regs[BPF_REG_3]
42 #define BPF_R4	regs[BPF_REG_4]
43 #define BPF_R5	regs[BPF_REG_5]
44 #define BPF_R6	regs[BPF_REG_6]
45 #define BPF_R7	regs[BPF_REG_7]
46 #define BPF_R8	regs[BPF_REG_8]
47 #define BPF_R9	regs[BPF_REG_9]
48 #define BPF_R10	regs[BPF_REG_10]
49 
50 /* Named registers */
51 #define DST	regs[insn->dst_reg]
52 #define SRC	regs[insn->src_reg]
53 #define FP	regs[BPF_REG_FP]
54 #define ARG1	regs[BPF_REG_ARG1]
55 #define CTX	regs[BPF_REG_CTX]
56 #define IMM	insn->imm
57 
58 /* No hurry in this branch
59  *
60  * Exported for the bpf jit load helper.
61  */
62 void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
63 {
64 	u8 *ptr = NULL;
65 
66 	if (k >= SKF_NET_OFF)
67 		ptr = skb_network_header(skb) + k - SKF_NET_OFF;
68 	else if (k >= SKF_LL_OFF)
69 		ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
70 
71 	if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
72 		return ptr;
73 
74 	return NULL;
75 }
76 
77 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
78 {
79 	gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
80 	struct bpf_prog_aux *aux;
81 	struct bpf_prog *fp;
82 
83 	size = round_up(size, PAGE_SIZE);
84 	fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
85 	if (fp == NULL)
86 		return NULL;
87 
88 	aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
89 	if (aux == NULL) {
90 		vfree(fp);
91 		return NULL;
92 	}
93 
94 	fp->pages = size / PAGE_SIZE;
95 	fp->aux = aux;
96 	fp->aux->prog = fp;
97 	fp->jit_requested = ebpf_jit_enabled();
98 
99 	INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode);
100 
101 	return fp;
102 }
103 EXPORT_SYMBOL_GPL(bpf_prog_alloc);
104 
105 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
106 				  gfp_t gfp_extra_flags)
107 {
108 	gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
109 	struct bpf_prog *fp;
110 	u32 pages, delta;
111 	int ret;
112 
113 	BUG_ON(fp_old == NULL);
114 
115 	size = round_up(size, PAGE_SIZE);
116 	pages = size / PAGE_SIZE;
117 	if (pages <= fp_old->pages)
118 		return fp_old;
119 
120 	delta = pages - fp_old->pages;
121 	ret = __bpf_prog_charge(fp_old->aux->user, delta);
122 	if (ret)
123 		return NULL;
124 
125 	fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
126 	if (fp == NULL) {
127 		__bpf_prog_uncharge(fp_old->aux->user, delta);
128 	} else {
129 		memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
130 		fp->pages = pages;
131 		fp->aux->prog = fp;
132 
133 		/* We keep fp->aux from fp_old around in the new
134 		 * reallocated structure.
135 		 */
136 		fp_old->aux = NULL;
137 		__bpf_prog_free(fp_old);
138 	}
139 
140 	return fp;
141 }
142 
143 void __bpf_prog_free(struct bpf_prog *fp)
144 {
145 	kfree(fp->aux);
146 	vfree(fp);
147 }
148 
149 int bpf_prog_calc_tag(struct bpf_prog *fp)
150 {
151 	const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64);
152 	u32 raw_size = bpf_prog_tag_scratch_size(fp);
153 	u32 digest[SHA_DIGEST_WORDS];
154 	u32 ws[SHA_WORKSPACE_WORDS];
155 	u32 i, bsize, psize, blocks;
156 	struct bpf_insn *dst;
157 	bool was_ld_map;
158 	u8 *raw, *todo;
159 	__be32 *result;
160 	__be64 *bits;
161 
162 	raw = vmalloc(raw_size);
163 	if (!raw)
164 		return -ENOMEM;
165 
166 	sha_init(digest);
167 	memset(ws, 0, sizeof(ws));
168 
169 	/* We need to take out the map fd for the digest calculation
170 	 * since they are unstable from user space side.
171 	 */
172 	dst = (void *)raw;
173 	for (i = 0, was_ld_map = false; i < fp->len; i++) {
174 		dst[i] = fp->insnsi[i];
175 		if (!was_ld_map &&
176 		    dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
177 		    dst[i].src_reg == BPF_PSEUDO_MAP_FD) {
178 			was_ld_map = true;
179 			dst[i].imm = 0;
180 		} else if (was_ld_map &&
181 			   dst[i].code == 0 &&
182 			   dst[i].dst_reg == 0 &&
183 			   dst[i].src_reg == 0 &&
184 			   dst[i].off == 0) {
185 			was_ld_map = false;
186 			dst[i].imm = 0;
187 		} else {
188 			was_ld_map = false;
189 		}
190 	}
191 
192 	psize = bpf_prog_insn_size(fp);
193 	memset(&raw[psize], 0, raw_size - psize);
194 	raw[psize++] = 0x80;
195 
196 	bsize  = round_up(psize, SHA_MESSAGE_BYTES);
197 	blocks = bsize / SHA_MESSAGE_BYTES;
198 	todo   = raw;
199 	if (bsize - psize >= sizeof(__be64)) {
200 		bits = (__be64 *)(todo + bsize - sizeof(__be64));
201 	} else {
202 		bits = (__be64 *)(todo + bsize + bits_offset);
203 		blocks++;
204 	}
205 	*bits = cpu_to_be64((psize - 1) << 3);
206 
207 	while (blocks--) {
208 		sha_transform(digest, todo, ws);
209 		todo += SHA_MESSAGE_BYTES;
210 	}
211 
212 	result = (__force __be32 *)digest;
213 	for (i = 0; i < SHA_DIGEST_WORDS; i++)
214 		result[i] = cpu_to_be32(digest[i]);
215 	memcpy(fp->tag, result, sizeof(fp->tag));
216 
217 	vfree(raw);
218 	return 0;
219 }
220 
221 static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
222 {
223 	struct bpf_insn *insn = prog->insnsi;
224 	u32 i, insn_cnt = prog->len;
225 	bool pseudo_call;
226 	u8 code;
227 	int off;
228 
229 	for (i = 0; i < insn_cnt; i++, insn++) {
230 		code = insn->code;
231 		if (BPF_CLASS(code) != BPF_JMP)
232 			continue;
233 		if (BPF_OP(code) == BPF_EXIT)
234 			continue;
235 		if (BPF_OP(code) == BPF_CALL) {
236 			if (insn->src_reg == BPF_PSEUDO_CALL)
237 				pseudo_call = true;
238 			else
239 				continue;
240 		} else {
241 			pseudo_call = false;
242 		}
243 		off = pseudo_call ? insn->imm : insn->off;
244 
245 		/* Adjust offset of jmps if we cross boundaries. */
246 		if (i < pos && i + off + 1 > pos)
247 			off += delta;
248 		else if (i > pos + delta && i + off + 1 <= pos + delta)
249 			off -= delta;
250 
251 		if (pseudo_call)
252 			insn->imm = off;
253 		else
254 			insn->off = off;
255 	}
256 }
257 
258 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
259 				       const struct bpf_insn *patch, u32 len)
260 {
261 	u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
262 	struct bpf_prog *prog_adj;
263 
264 	/* Since our patchlet doesn't expand the image, we're done. */
265 	if (insn_delta == 0) {
266 		memcpy(prog->insnsi + off, patch, sizeof(*patch));
267 		return prog;
268 	}
269 
270 	insn_adj_cnt = prog->len + insn_delta;
271 
272 	/* Several new instructions need to be inserted. Make room
273 	 * for them. Likely, there's no need for a new allocation as
274 	 * last page could have large enough tailroom.
275 	 */
276 	prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
277 				    GFP_USER);
278 	if (!prog_adj)
279 		return NULL;
280 
281 	prog_adj->len = insn_adj_cnt;
282 
283 	/* Patching happens in 3 steps:
284 	 *
285 	 * 1) Move over tail of insnsi from next instruction onwards,
286 	 *    so we can patch the single target insn with one or more
287 	 *    new ones (patching is always from 1 to n insns, n > 0).
288 	 * 2) Inject new instructions at the target location.
289 	 * 3) Adjust branch offsets if necessary.
290 	 */
291 	insn_rest = insn_adj_cnt - off - len;
292 
293 	memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
294 		sizeof(*patch) * insn_rest);
295 	memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
296 
297 	bpf_adj_branches(prog_adj, off, insn_delta);
298 
299 	return prog_adj;
300 }
301 
302 #ifdef CONFIG_BPF_JIT
303 /* All BPF JIT sysctl knobs here. */
304 int bpf_jit_enable   __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
305 int bpf_jit_harden   __read_mostly;
306 int bpf_jit_kallsyms __read_mostly;
307 
308 static __always_inline void
309 bpf_get_prog_addr_region(const struct bpf_prog *prog,
310 			 unsigned long *symbol_start,
311 			 unsigned long *symbol_end)
312 {
313 	const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
314 	unsigned long addr = (unsigned long)hdr;
315 
316 	WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
317 
318 	*symbol_start = addr;
319 	*symbol_end   = addr + hdr->pages * PAGE_SIZE;
320 }
321 
322 static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
323 {
324 	const char *end = sym + KSYM_NAME_LEN;
325 
326 	BUILD_BUG_ON(sizeof("bpf_prog_") +
327 		     sizeof(prog->tag) * 2 +
328 		     /* name has been null terminated.
329 		      * We should need +1 for the '_' preceding
330 		      * the name.  However, the null character
331 		      * is double counted between the name and the
332 		      * sizeof("bpf_prog_") above, so we omit
333 		      * the +1 here.
334 		      */
335 		     sizeof(prog->aux->name) > KSYM_NAME_LEN);
336 
337 	sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
338 	sym  = bin2hex(sym, prog->tag, sizeof(prog->tag));
339 	if (prog->aux->name[0])
340 		snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
341 	else
342 		*sym = 0;
343 }
344 
345 static __always_inline unsigned long
346 bpf_get_prog_addr_start(struct latch_tree_node *n)
347 {
348 	unsigned long symbol_start, symbol_end;
349 	const struct bpf_prog_aux *aux;
350 
351 	aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
352 	bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
353 
354 	return symbol_start;
355 }
356 
357 static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
358 					  struct latch_tree_node *b)
359 {
360 	return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
361 }
362 
363 static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
364 {
365 	unsigned long val = (unsigned long)key;
366 	unsigned long symbol_start, symbol_end;
367 	const struct bpf_prog_aux *aux;
368 
369 	aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
370 	bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
371 
372 	if (val < symbol_start)
373 		return -1;
374 	if (val >= symbol_end)
375 		return  1;
376 
377 	return 0;
378 }
379 
380 static const struct latch_tree_ops bpf_tree_ops = {
381 	.less	= bpf_tree_less,
382 	.comp	= bpf_tree_comp,
383 };
384 
385 static DEFINE_SPINLOCK(bpf_lock);
386 static LIST_HEAD(bpf_kallsyms);
387 static struct latch_tree_root bpf_tree __cacheline_aligned;
388 
389 static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
390 {
391 	WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
392 	list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
393 	latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
394 }
395 
396 static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
397 {
398 	if (list_empty(&aux->ksym_lnode))
399 		return;
400 
401 	latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
402 	list_del_rcu(&aux->ksym_lnode);
403 }
404 
405 static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
406 {
407 	return fp->jited && !bpf_prog_was_classic(fp);
408 }
409 
410 static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
411 {
412 	return list_empty(&fp->aux->ksym_lnode) ||
413 	       fp->aux->ksym_lnode.prev == LIST_POISON2;
414 }
415 
416 void bpf_prog_kallsyms_add(struct bpf_prog *fp)
417 {
418 	if (!bpf_prog_kallsyms_candidate(fp) ||
419 	    !capable(CAP_SYS_ADMIN))
420 		return;
421 
422 	spin_lock_bh(&bpf_lock);
423 	bpf_prog_ksym_node_add(fp->aux);
424 	spin_unlock_bh(&bpf_lock);
425 }
426 
427 void bpf_prog_kallsyms_del(struct bpf_prog *fp)
428 {
429 	if (!bpf_prog_kallsyms_candidate(fp))
430 		return;
431 
432 	spin_lock_bh(&bpf_lock);
433 	bpf_prog_ksym_node_del(fp->aux);
434 	spin_unlock_bh(&bpf_lock);
435 }
436 
437 static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
438 {
439 	struct latch_tree_node *n;
440 
441 	if (!bpf_jit_kallsyms_enabled())
442 		return NULL;
443 
444 	n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
445 	return n ?
446 	       container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
447 	       NULL;
448 }
449 
450 const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
451 				 unsigned long *off, char *sym)
452 {
453 	unsigned long symbol_start, symbol_end;
454 	struct bpf_prog *prog;
455 	char *ret = NULL;
456 
457 	rcu_read_lock();
458 	prog = bpf_prog_kallsyms_find(addr);
459 	if (prog) {
460 		bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
461 		bpf_get_prog_name(prog, sym);
462 
463 		ret = sym;
464 		if (size)
465 			*size = symbol_end - symbol_start;
466 		if (off)
467 			*off  = addr - symbol_start;
468 	}
469 	rcu_read_unlock();
470 
471 	return ret;
472 }
473 
474 bool is_bpf_text_address(unsigned long addr)
475 {
476 	bool ret;
477 
478 	rcu_read_lock();
479 	ret = bpf_prog_kallsyms_find(addr) != NULL;
480 	rcu_read_unlock();
481 
482 	return ret;
483 }
484 
485 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
486 		    char *sym)
487 {
488 	unsigned long symbol_start, symbol_end;
489 	struct bpf_prog_aux *aux;
490 	unsigned int it = 0;
491 	int ret = -ERANGE;
492 
493 	if (!bpf_jit_kallsyms_enabled())
494 		return ret;
495 
496 	rcu_read_lock();
497 	list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
498 		if (it++ != symnum)
499 			continue;
500 
501 		bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
502 		bpf_get_prog_name(aux->prog, sym);
503 
504 		*value = symbol_start;
505 		*type  = BPF_SYM_ELF_TYPE;
506 
507 		ret = 0;
508 		break;
509 	}
510 	rcu_read_unlock();
511 
512 	return ret;
513 }
514 
515 struct bpf_binary_header *
516 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
517 		     unsigned int alignment,
518 		     bpf_jit_fill_hole_t bpf_fill_ill_insns)
519 {
520 	struct bpf_binary_header *hdr;
521 	unsigned int size, hole, start;
522 
523 	/* Most of BPF filters are really small, but if some of them
524 	 * fill a page, allow at least 128 extra bytes to insert a
525 	 * random section of illegal instructions.
526 	 */
527 	size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
528 	hdr = module_alloc(size);
529 	if (hdr == NULL)
530 		return NULL;
531 
532 	/* Fill space with illegal/arch-dep instructions. */
533 	bpf_fill_ill_insns(hdr, size);
534 
535 	hdr->pages = size / PAGE_SIZE;
536 	hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
537 		     PAGE_SIZE - sizeof(*hdr));
538 	start = (get_random_int() % hole) & ~(alignment - 1);
539 
540 	/* Leave a random number of instructions before BPF code. */
541 	*image_ptr = &hdr->image[start];
542 
543 	return hdr;
544 }
545 
546 void bpf_jit_binary_free(struct bpf_binary_header *hdr)
547 {
548 	module_memfree(hdr);
549 }
550 
551 /* This symbol is only overridden by archs that have different
552  * requirements than the usual eBPF JITs, f.e. when they only
553  * implement cBPF JIT, do not set images read-only, etc.
554  */
555 void __weak bpf_jit_free(struct bpf_prog *fp)
556 {
557 	if (fp->jited) {
558 		struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
559 
560 		bpf_jit_binary_unlock_ro(hdr);
561 		bpf_jit_binary_free(hdr);
562 
563 		WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
564 	}
565 
566 	bpf_prog_unlock_free(fp);
567 }
568 
569 static int bpf_jit_blind_insn(const struct bpf_insn *from,
570 			      const struct bpf_insn *aux,
571 			      struct bpf_insn *to_buff)
572 {
573 	struct bpf_insn *to = to_buff;
574 	u32 imm_rnd = get_random_int();
575 	s16 off;
576 
577 	BUILD_BUG_ON(BPF_REG_AX  + 1 != MAX_BPF_JIT_REG);
578 	BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
579 
580 	if (from->imm == 0 &&
581 	    (from->code == (BPF_ALU   | BPF_MOV | BPF_K) ||
582 	     from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
583 		*to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
584 		goto out;
585 	}
586 
587 	switch (from->code) {
588 	case BPF_ALU | BPF_ADD | BPF_K:
589 	case BPF_ALU | BPF_SUB | BPF_K:
590 	case BPF_ALU | BPF_AND | BPF_K:
591 	case BPF_ALU | BPF_OR  | BPF_K:
592 	case BPF_ALU | BPF_XOR | BPF_K:
593 	case BPF_ALU | BPF_MUL | BPF_K:
594 	case BPF_ALU | BPF_MOV | BPF_K:
595 	case BPF_ALU | BPF_DIV | BPF_K:
596 	case BPF_ALU | BPF_MOD | BPF_K:
597 		*to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
598 		*to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
599 		*to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
600 		break;
601 
602 	case BPF_ALU64 | BPF_ADD | BPF_K:
603 	case BPF_ALU64 | BPF_SUB | BPF_K:
604 	case BPF_ALU64 | BPF_AND | BPF_K:
605 	case BPF_ALU64 | BPF_OR  | BPF_K:
606 	case BPF_ALU64 | BPF_XOR | BPF_K:
607 	case BPF_ALU64 | BPF_MUL | BPF_K:
608 	case BPF_ALU64 | BPF_MOV | BPF_K:
609 	case BPF_ALU64 | BPF_DIV | BPF_K:
610 	case BPF_ALU64 | BPF_MOD | BPF_K:
611 		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
612 		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
613 		*to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
614 		break;
615 
616 	case BPF_JMP | BPF_JEQ  | BPF_K:
617 	case BPF_JMP | BPF_JNE  | BPF_K:
618 	case BPF_JMP | BPF_JGT  | BPF_K:
619 	case BPF_JMP | BPF_JLT  | BPF_K:
620 	case BPF_JMP | BPF_JGE  | BPF_K:
621 	case BPF_JMP | BPF_JLE  | BPF_K:
622 	case BPF_JMP | BPF_JSGT | BPF_K:
623 	case BPF_JMP | BPF_JSLT | BPF_K:
624 	case BPF_JMP | BPF_JSGE | BPF_K:
625 	case BPF_JMP | BPF_JSLE | BPF_K:
626 	case BPF_JMP | BPF_JSET | BPF_K:
627 		/* Accommodate for extra offset in case of a backjump. */
628 		off = from->off;
629 		if (off < 0)
630 			off -= 2;
631 		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
632 		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
633 		*to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
634 		break;
635 
636 	case BPF_LD | BPF_ABS | BPF_W:
637 	case BPF_LD | BPF_ABS | BPF_H:
638 	case BPF_LD | BPF_ABS | BPF_B:
639 		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
640 		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
641 		*to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
642 		break;
643 
644 	case BPF_LD | BPF_IND | BPF_W:
645 	case BPF_LD | BPF_IND | BPF_H:
646 	case BPF_LD | BPF_IND | BPF_B:
647 		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
648 		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
649 		*to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
650 		*to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
651 		break;
652 
653 	case BPF_LD | BPF_IMM | BPF_DW:
654 		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
655 		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
656 		*to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
657 		*to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
658 		break;
659 	case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
660 		*to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
661 		*to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
662 		*to++ = BPF_ALU64_REG(BPF_OR,  aux[0].dst_reg, BPF_REG_AX);
663 		break;
664 
665 	case BPF_ST | BPF_MEM | BPF_DW:
666 	case BPF_ST | BPF_MEM | BPF_W:
667 	case BPF_ST | BPF_MEM | BPF_H:
668 	case BPF_ST | BPF_MEM | BPF_B:
669 		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
670 		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
671 		*to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
672 		break;
673 	}
674 out:
675 	return to - to_buff;
676 }
677 
678 static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
679 					      gfp_t gfp_extra_flags)
680 {
681 	gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
682 	struct bpf_prog *fp;
683 
684 	fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
685 	if (fp != NULL) {
686 		/* aux->prog still points to the fp_other one, so
687 		 * when promoting the clone to the real program,
688 		 * this still needs to be adapted.
689 		 */
690 		memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
691 	}
692 
693 	return fp;
694 }
695 
696 static void bpf_prog_clone_free(struct bpf_prog *fp)
697 {
698 	/* aux was stolen by the other clone, so we cannot free
699 	 * it from this path! It will be freed eventually by the
700 	 * other program on release.
701 	 *
702 	 * At this point, we don't need a deferred release since
703 	 * clone is guaranteed to not be locked.
704 	 */
705 	fp->aux = NULL;
706 	__bpf_prog_free(fp);
707 }
708 
709 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
710 {
711 	/* We have to repoint aux->prog to self, as we don't
712 	 * know whether fp here is the clone or the original.
713 	 */
714 	fp->aux->prog = fp;
715 	bpf_prog_clone_free(fp_other);
716 }
717 
718 struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
719 {
720 	struct bpf_insn insn_buff[16], aux[2];
721 	struct bpf_prog *clone, *tmp;
722 	int insn_delta, insn_cnt;
723 	struct bpf_insn *insn;
724 	int i, rewritten;
725 
726 	if (!bpf_jit_blinding_enabled(prog) || prog->blinded)
727 		return prog;
728 
729 	clone = bpf_prog_clone_create(prog, GFP_USER);
730 	if (!clone)
731 		return ERR_PTR(-ENOMEM);
732 
733 	insn_cnt = clone->len;
734 	insn = clone->insnsi;
735 
736 	for (i = 0; i < insn_cnt; i++, insn++) {
737 		/* We temporarily need to hold the original ld64 insn
738 		 * so that we can still access the first part in the
739 		 * second blinding run.
740 		 */
741 		if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
742 		    insn[1].code == 0)
743 			memcpy(aux, insn, sizeof(aux));
744 
745 		rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
746 		if (!rewritten)
747 			continue;
748 
749 		tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
750 		if (!tmp) {
751 			/* Patching may have repointed aux->prog during
752 			 * realloc from the original one, so we need to
753 			 * fix it up here on error.
754 			 */
755 			bpf_jit_prog_release_other(prog, clone);
756 			return ERR_PTR(-ENOMEM);
757 		}
758 
759 		clone = tmp;
760 		insn_delta = rewritten - 1;
761 
762 		/* Walk new program and skip insns we just inserted. */
763 		insn = clone->insnsi + i + insn_delta;
764 		insn_cnt += insn_delta;
765 		i        += insn_delta;
766 	}
767 
768 	clone->blinded = 1;
769 	return clone;
770 }
771 #endif /* CONFIG_BPF_JIT */
772 
773 /* Base function for offset calculation. Needs to go into .text section,
774  * therefore keeping it non-static as well; will also be used by JITs
775  * anyway later on, so do not let the compiler omit it. This also needs
776  * to go into kallsyms for correlation from e.g. bpftool, so naming
777  * must not change.
778  */
779 noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
780 {
781 	return 0;
782 }
783 EXPORT_SYMBOL_GPL(__bpf_call_base);
784 
785 /* All UAPI available opcodes. */
786 #define BPF_INSN_MAP(INSN_2, INSN_3)		\
787 	/* 32 bit ALU operations. */		\
788 	/*   Register based. */			\
789 	INSN_3(ALU, ADD, X),			\
790 	INSN_3(ALU, SUB, X),			\
791 	INSN_3(ALU, AND, X),			\
792 	INSN_3(ALU, OR,  X),			\
793 	INSN_3(ALU, LSH, X),			\
794 	INSN_3(ALU, RSH, X),			\
795 	INSN_3(ALU, XOR, X),			\
796 	INSN_3(ALU, MUL, X),			\
797 	INSN_3(ALU, MOV, X),			\
798 	INSN_3(ALU, DIV, X),			\
799 	INSN_3(ALU, MOD, X),			\
800 	INSN_2(ALU, NEG),			\
801 	INSN_3(ALU, END, TO_BE),		\
802 	INSN_3(ALU, END, TO_LE),		\
803 	/*   Immediate based. */		\
804 	INSN_3(ALU, ADD, K),			\
805 	INSN_3(ALU, SUB, K),			\
806 	INSN_3(ALU, AND, K),			\
807 	INSN_3(ALU, OR,  K),			\
808 	INSN_3(ALU, LSH, K),			\
809 	INSN_3(ALU, RSH, K),			\
810 	INSN_3(ALU, XOR, K),			\
811 	INSN_3(ALU, MUL, K),			\
812 	INSN_3(ALU, MOV, K),			\
813 	INSN_3(ALU, DIV, K),			\
814 	INSN_3(ALU, MOD, K),			\
815 	/* 64 bit ALU operations. */		\
816 	/*   Register based. */			\
817 	INSN_3(ALU64, ADD,  X),			\
818 	INSN_3(ALU64, SUB,  X),			\
819 	INSN_3(ALU64, AND,  X),			\
820 	INSN_3(ALU64, OR,   X),			\
821 	INSN_3(ALU64, LSH,  X),			\
822 	INSN_3(ALU64, RSH,  X),			\
823 	INSN_3(ALU64, XOR,  X),			\
824 	INSN_3(ALU64, MUL,  X),			\
825 	INSN_3(ALU64, MOV,  X),			\
826 	INSN_3(ALU64, ARSH, X),			\
827 	INSN_3(ALU64, DIV,  X),			\
828 	INSN_3(ALU64, MOD,  X),			\
829 	INSN_2(ALU64, NEG),			\
830 	/*   Immediate based. */		\
831 	INSN_3(ALU64, ADD,  K),			\
832 	INSN_3(ALU64, SUB,  K),			\
833 	INSN_3(ALU64, AND,  K),			\
834 	INSN_3(ALU64, OR,   K),			\
835 	INSN_3(ALU64, LSH,  K),			\
836 	INSN_3(ALU64, RSH,  K),			\
837 	INSN_3(ALU64, XOR,  K),			\
838 	INSN_3(ALU64, MUL,  K),			\
839 	INSN_3(ALU64, MOV,  K),			\
840 	INSN_3(ALU64, ARSH, K),			\
841 	INSN_3(ALU64, DIV,  K),			\
842 	INSN_3(ALU64, MOD,  K),			\
843 	/* Call instruction. */			\
844 	INSN_2(JMP, CALL),			\
845 	/* Exit instruction. */			\
846 	INSN_2(JMP, EXIT),			\
847 	/* Jump instructions. */		\
848 	/*   Register based. */			\
849 	INSN_3(JMP, JEQ,  X),			\
850 	INSN_3(JMP, JNE,  X),			\
851 	INSN_3(JMP, JGT,  X),			\
852 	INSN_3(JMP, JLT,  X),			\
853 	INSN_3(JMP, JGE,  X),			\
854 	INSN_3(JMP, JLE,  X),			\
855 	INSN_3(JMP, JSGT, X),			\
856 	INSN_3(JMP, JSLT, X),			\
857 	INSN_3(JMP, JSGE, X),			\
858 	INSN_3(JMP, JSLE, X),			\
859 	INSN_3(JMP, JSET, X),			\
860 	/*   Immediate based. */		\
861 	INSN_3(JMP, JEQ,  K),			\
862 	INSN_3(JMP, JNE,  K),			\
863 	INSN_3(JMP, JGT,  K),			\
864 	INSN_3(JMP, JLT,  K),			\
865 	INSN_3(JMP, JGE,  K),			\
866 	INSN_3(JMP, JLE,  K),			\
867 	INSN_3(JMP, JSGT, K),			\
868 	INSN_3(JMP, JSLT, K),			\
869 	INSN_3(JMP, JSGE, K),			\
870 	INSN_3(JMP, JSLE, K),			\
871 	INSN_3(JMP, JSET, K),			\
872 	INSN_2(JMP, JA),			\
873 	/* Store instructions. */		\
874 	/*   Register based. */			\
875 	INSN_3(STX, MEM,  B),			\
876 	INSN_3(STX, MEM,  H),			\
877 	INSN_3(STX, MEM,  W),			\
878 	INSN_3(STX, MEM,  DW),			\
879 	INSN_3(STX, XADD, W),			\
880 	INSN_3(STX, XADD, DW),			\
881 	/*   Immediate based. */		\
882 	INSN_3(ST, MEM, B),			\
883 	INSN_3(ST, MEM, H),			\
884 	INSN_3(ST, MEM, W),			\
885 	INSN_3(ST, MEM, DW),			\
886 	/* Load instructions. */		\
887 	/*   Register based. */			\
888 	INSN_3(LDX, MEM, B),			\
889 	INSN_3(LDX, MEM, H),			\
890 	INSN_3(LDX, MEM, W),			\
891 	INSN_3(LDX, MEM, DW),			\
892 	/*   Immediate based. */		\
893 	INSN_3(LD, IMM, DW),			\
894 	/*   Misc (old cBPF carry-over). */	\
895 	INSN_3(LD, ABS, B),			\
896 	INSN_3(LD, ABS, H),			\
897 	INSN_3(LD, ABS, W),			\
898 	INSN_3(LD, IND, B),			\
899 	INSN_3(LD, IND, H),			\
900 	INSN_3(LD, IND, W)
901 
902 bool bpf_opcode_in_insntable(u8 code)
903 {
904 #define BPF_INSN_2_TBL(x, y)    [BPF_##x | BPF_##y] = true
905 #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true
906 	static const bool public_insntable[256] = {
907 		[0 ... 255] = false,
908 		/* Now overwrite non-defaults ... */
909 		BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL),
910 	};
911 #undef BPF_INSN_3_TBL
912 #undef BPF_INSN_2_TBL
913 	return public_insntable[code];
914 }
915 
916 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
917 /**
918  *	__bpf_prog_run - run eBPF program on a given context
919  *	@ctx: is the data we are operating on
920  *	@insn: is the array of eBPF instructions
921  *
922  * Decode and execute eBPF instructions.
923  */
924 static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
925 {
926 	u64 tmp;
927 #define BPF_INSN_2_LBL(x, y)    [BPF_##x | BPF_##y] = &&x##_##y
928 #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
929 	static const void *jumptable[256] = {
930 		[0 ... 255] = &&default_label,
931 		/* Now overwrite non-defaults ... */
932 		BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL),
933 		/* Non-UAPI available opcodes. */
934 		[BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
935 		[BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
936 	};
937 #undef BPF_INSN_3_LBL
938 #undef BPF_INSN_2_LBL
939 	u32 tail_call_cnt = 0;
940 	void *ptr;
941 	int off;
942 
943 #define CONT	 ({ insn++; goto select_insn; })
944 #define CONT_JMP ({ insn++; goto select_insn; })
945 
946 select_insn:
947 	goto *jumptable[insn->code];
948 
949 	/* ALU */
950 #define ALU(OPCODE, OP)			\
951 	ALU64_##OPCODE##_X:		\
952 		DST = DST OP SRC;	\
953 		CONT;			\
954 	ALU_##OPCODE##_X:		\
955 		DST = (u32) DST OP (u32) SRC;	\
956 		CONT;			\
957 	ALU64_##OPCODE##_K:		\
958 		DST = DST OP IMM;		\
959 		CONT;			\
960 	ALU_##OPCODE##_K:		\
961 		DST = (u32) DST OP (u32) IMM;	\
962 		CONT;
963 
964 	ALU(ADD,  +)
965 	ALU(SUB,  -)
966 	ALU(AND,  &)
967 	ALU(OR,   |)
968 	ALU(LSH, <<)
969 	ALU(RSH, >>)
970 	ALU(XOR,  ^)
971 	ALU(MUL,  *)
972 #undef ALU
973 	ALU_NEG:
974 		DST = (u32) -DST;
975 		CONT;
976 	ALU64_NEG:
977 		DST = -DST;
978 		CONT;
979 	ALU_MOV_X:
980 		DST = (u32) SRC;
981 		CONT;
982 	ALU_MOV_K:
983 		DST = (u32) IMM;
984 		CONT;
985 	ALU64_MOV_X:
986 		DST = SRC;
987 		CONT;
988 	ALU64_MOV_K:
989 		DST = IMM;
990 		CONT;
991 	LD_IMM_DW:
992 		DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
993 		insn++;
994 		CONT;
995 	ALU64_ARSH_X:
996 		(*(s64 *) &DST) >>= SRC;
997 		CONT;
998 	ALU64_ARSH_K:
999 		(*(s64 *) &DST) >>= IMM;
1000 		CONT;
1001 	ALU64_MOD_X:
1002 		div64_u64_rem(DST, SRC, &tmp);
1003 		DST = tmp;
1004 		CONT;
1005 	ALU_MOD_X:
1006 		tmp = (u32) DST;
1007 		DST = do_div(tmp, (u32) SRC);
1008 		CONT;
1009 	ALU64_MOD_K:
1010 		div64_u64_rem(DST, IMM, &tmp);
1011 		DST = tmp;
1012 		CONT;
1013 	ALU_MOD_K:
1014 		tmp = (u32) DST;
1015 		DST = do_div(tmp, (u32) IMM);
1016 		CONT;
1017 	ALU64_DIV_X:
1018 		DST = div64_u64(DST, SRC);
1019 		CONT;
1020 	ALU_DIV_X:
1021 		tmp = (u32) DST;
1022 		do_div(tmp, (u32) SRC);
1023 		DST = (u32) tmp;
1024 		CONT;
1025 	ALU64_DIV_K:
1026 		DST = div64_u64(DST, IMM);
1027 		CONT;
1028 	ALU_DIV_K:
1029 		tmp = (u32) DST;
1030 		do_div(tmp, (u32) IMM);
1031 		DST = (u32) tmp;
1032 		CONT;
1033 	ALU_END_TO_BE:
1034 		switch (IMM) {
1035 		case 16:
1036 			DST = (__force u16) cpu_to_be16(DST);
1037 			break;
1038 		case 32:
1039 			DST = (__force u32) cpu_to_be32(DST);
1040 			break;
1041 		case 64:
1042 			DST = (__force u64) cpu_to_be64(DST);
1043 			break;
1044 		}
1045 		CONT;
1046 	ALU_END_TO_LE:
1047 		switch (IMM) {
1048 		case 16:
1049 			DST = (__force u16) cpu_to_le16(DST);
1050 			break;
1051 		case 32:
1052 			DST = (__force u32) cpu_to_le32(DST);
1053 			break;
1054 		case 64:
1055 			DST = (__force u64) cpu_to_le64(DST);
1056 			break;
1057 		}
1058 		CONT;
1059 
1060 	/* CALL */
1061 	JMP_CALL:
1062 		/* Function call scratches BPF_R1-BPF_R5 registers,
1063 		 * preserves BPF_R6-BPF_R9, and stores return value
1064 		 * into BPF_R0.
1065 		 */
1066 		BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1067 						       BPF_R4, BPF_R5);
1068 		CONT;
1069 
1070 	JMP_CALL_ARGS:
1071 		BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
1072 							    BPF_R3, BPF_R4,
1073 							    BPF_R5,
1074 							    insn + insn->off + 1);
1075 		CONT;
1076 
1077 	JMP_TAIL_CALL: {
1078 		struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1079 		struct bpf_array *array = container_of(map, struct bpf_array, map);
1080 		struct bpf_prog *prog;
1081 		u32 index = BPF_R3;
1082 
1083 		if (unlikely(index >= array->map.max_entries))
1084 			goto out;
1085 		if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1086 			goto out;
1087 
1088 		tail_call_cnt++;
1089 
1090 		prog = READ_ONCE(array->ptrs[index]);
1091 		if (!prog)
1092 			goto out;
1093 
1094 		/* ARG1 at this point is guaranteed to point to CTX from
1095 		 * the verifier side due to the fact that the tail call is
1096 		 * handeled like a helper, that is, bpf_tail_call_proto,
1097 		 * where arg1_type is ARG_PTR_TO_CTX.
1098 		 */
1099 		insn = prog->insnsi;
1100 		goto select_insn;
1101 out:
1102 		CONT;
1103 	}
1104 	/* JMP */
1105 	JMP_JA:
1106 		insn += insn->off;
1107 		CONT;
1108 	JMP_JEQ_X:
1109 		if (DST == SRC) {
1110 			insn += insn->off;
1111 			CONT_JMP;
1112 		}
1113 		CONT;
1114 	JMP_JEQ_K:
1115 		if (DST == IMM) {
1116 			insn += insn->off;
1117 			CONT_JMP;
1118 		}
1119 		CONT;
1120 	JMP_JNE_X:
1121 		if (DST != SRC) {
1122 			insn += insn->off;
1123 			CONT_JMP;
1124 		}
1125 		CONT;
1126 	JMP_JNE_K:
1127 		if (DST != IMM) {
1128 			insn += insn->off;
1129 			CONT_JMP;
1130 		}
1131 		CONT;
1132 	JMP_JGT_X:
1133 		if (DST > SRC) {
1134 			insn += insn->off;
1135 			CONT_JMP;
1136 		}
1137 		CONT;
1138 	JMP_JGT_K:
1139 		if (DST > IMM) {
1140 			insn += insn->off;
1141 			CONT_JMP;
1142 		}
1143 		CONT;
1144 	JMP_JLT_X:
1145 		if (DST < SRC) {
1146 			insn += insn->off;
1147 			CONT_JMP;
1148 		}
1149 		CONT;
1150 	JMP_JLT_K:
1151 		if (DST < IMM) {
1152 			insn += insn->off;
1153 			CONT_JMP;
1154 		}
1155 		CONT;
1156 	JMP_JGE_X:
1157 		if (DST >= SRC) {
1158 			insn += insn->off;
1159 			CONT_JMP;
1160 		}
1161 		CONT;
1162 	JMP_JGE_K:
1163 		if (DST >= IMM) {
1164 			insn += insn->off;
1165 			CONT_JMP;
1166 		}
1167 		CONT;
1168 	JMP_JLE_X:
1169 		if (DST <= SRC) {
1170 			insn += insn->off;
1171 			CONT_JMP;
1172 		}
1173 		CONT;
1174 	JMP_JLE_K:
1175 		if (DST <= IMM) {
1176 			insn += insn->off;
1177 			CONT_JMP;
1178 		}
1179 		CONT;
1180 	JMP_JSGT_X:
1181 		if (((s64) DST) > ((s64) SRC)) {
1182 			insn += insn->off;
1183 			CONT_JMP;
1184 		}
1185 		CONT;
1186 	JMP_JSGT_K:
1187 		if (((s64) DST) > ((s64) IMM)) {
1188 			insn += insn->off;
1189 			CONT_JMP;
1190 		}
1191 		CONT;
1192 	JMP_JSLT_X:
1193 		if (((s64) DST) < ((s64) SRC)) {
1194 			insn += insn->off;
1195 			CONT_JMP;
1196 		}
1197 		CONT;
1198 	JMP_JSLT_K:
1199 		if (((s64) DST) < ((s64) IMM)) {
1200 			insn += insn->off;
1201 			CONT_JMP;
1202 		}
1203 		CONT;
1204 	JMP_JSGE_X:
1205 		if (((s64) DST) >= ((s64) SRC)) {
1206 			insn += insn->off;
1207 			CONT_JMP;
1208 		}
1209 		CONT;
1210 	JMP_JSGE_K:
1211 		if (((s64) DST) >= ((s64) IMM)) {
1212 			insn += insn->off;
1213 			CONT_JMP;
1214 		}
1215 		CONT;
1216 	JMP_JSLE_X:
1217 		if (((s64) DST) <= ((s64) SRC)) {
1218 			insn += insn->off;
1219 			CONT_JMP;
1220 		}
1221 		CONT;
1222 	JMP_JSLE_K:
1223 		if (((s64) DST) <= ((s64) IMM)) {
1224 			insn += insn->off;
1225 			CONT_JMP;
1226 		}
1227 		CONT;
1228 	JMP_JSET_X:
1229 		if (DST & SRC) {
1230 			insn += insn->off;
1231 			CONT_JMP;
1232 		}
1233 		CONT;
1234 	JMP_JSET_K:
1235 		if (DST & IMM) {
1236 			insn += insn->off;
1237 			CONT_JMP;
1238 		}
1239 		CONT;
1240 	JMP_EXIT:
1241 		return BPF_R0;
1242 
1243 	/* STX and ST and LDX*/
1244 #define LDST(SIZEOP, SIZE)						\
1245 	STX_MEM_##SIZEOP:						\
1246 		*(SIZE *)(unsigned long) (DST + insn->off) = SRC;	\
1247 		CONT;							\
1248 	ST_MEM_##SIZEOP:						\
1249 		*(SIZE *)(unsigned long) (DST + insn->off) = IMM;	\
1250 		CONT;							\
1251 	LDX_MEM_##SIZEOP:						\
1252 		DST = *(SIZE *)(unsigned long) (SRC + insn->off);	\
1253 		CONT;
1254 
1255 	LDST(B,   u8)
1256 	LDST(H,  u16)
1257 	LDST(W,  u32)
1258 	LDST(DW, u64)
1259 #undef LDST
1260 	STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1261 		atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1262 			   (DST + insn->off));
1263 		CONT;
1264 	STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1265 		atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1266 			     (DST + insn->off));
1267 		CONT;
1268 	LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
1269 		off = IMM;
1270 load_word:
1271 		/* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are only
1272 		 * appearing in the programs where ctx == skb
1273 		 * (see may_access_skb() in the verifier). All programs
1274 		 * keep 'ctx' in regs[BPF_REG_CTX] == BPF_R6,
1275 		 * bpf_convert_filter() saves it in BPF_R6, internal BPF
1276 		 * verifier will check that BPF_R6 == ctx.
1277 		 *
1278 		 * BPF_ABS and BPF_IND are wrappers of function calls,
1279 		 * so they scratch BPF_R1-BPF_R5 registers, preserve
1280 		 * BPF_R6-BPF_R9, and store return value into BPF_R0.
1281 		 *
1282 		 * Implicit input:
1283 		 *   ctx == skb == BPF_R6 == CTX
1284 		 *
1285 		 * Explicit input:
1286 		 *   SRC == any register
1287 		 *   IMM == 32-bit immediate
1288 		 *
1289 		 * Output:
1290 		 *   BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
1291 		 */
1292 
1293 		ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
1294 		if (likely(ptr != NULL)) {
1295 			BPF_R0 = get_unaligned_be32(ptr);
1296 			CONT;
1297 		}
1298 
1299 		return 0;
1300 	LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
1301 		off = IMM;
1302 load_half:
1303 		ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
1304 		if (likely(ptr != NULL)) {
1305 			BPF_R0 = get_unaligned_be16(ptr);
1306 			CONT;
1307 		}
1308 
1309 		return 0;
1310 	LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
1311 		off = IMM;
1312 load_byte:
1313 		ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
1314 		if (likely(ptr != NULL)) {
1315 			BPF_R0 = *(u8 *)ptr;
1316 			CONT;
1317 		}
1318 
1319 		return 0;
1320 	LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
1321 		off = IMM + SRC;
1322 		goto load_word;
1323 	LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
1324 		off = IMM + SRC;
1325 		goto load_half;
1326 	LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
1327 		off = IMM + SRC;
1328 		goto load_byte;
1329 
1330 	default_label:
1331 		/* If we ever reach this, we have a bug somewhere. Die hard here
1332 		 * instead of just returning 0; we could be somewhere in a subprog,
1333 		 * so execution could continue otherwise which we do /not/ want.
1334 		 *
1335 		 * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable().
1336 		 */
1337 		pr_warn("BPF interpreter: unknown opcode %02x\n", insn->code);
1338 		BUG_ON(1);
1339 		return 0;
1340 }
1341 STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1342 
1343 #define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1344 #define DEFINE_BPF_PROG_RUN(stack_size) \
1345 static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1346 { \
1347 	u64 stack[stack_size / sizeof(u64)]; \
1348 	u64 regs[MAX_BPF_REG]; \
1349 \
1350 	FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1351 	ARG1 = (u64) (unsigned long) ctx; \
1352 	return ___bpf_prog_run(regs, insn, stack); \
1353 }
1354 
1355 #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1356 #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1357 static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
1358 				      const struct bpf_insn *insn) \
1359 { \
1360 	u64 stack[stack_size / sizeof(u64)]; \
1361 	u64 regs[MAX_BPF_REG]; \
1362 \
1363 	FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1364 	BPF_R1 = r1; \
1365 	BPF_R2 = r2; \
1366 	BPF_R3 = r3; \
1367 	BPF_R4 = r4; \
1368 	BPF_R5 = r5; \
1369 	return ___bpf_prog_run(regs, insn, stack); \
1370 }
1371 
1372 #define EVAL1(FN, X) FN(X)
1373 #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1374 #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1375 #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1376 #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1377 #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1378 
1379 EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1380 EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1381 EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1382 
1383 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
1384 EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
1385 EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
1386 
1387 #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1388 
1389 static unsigned int (*interpreters[])(const void *ctx,
1390 				      const struct bpf_insn *insn) = {
1391 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1392 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1393 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1394 };
1395 #undef PROG_NAME_LIST
1396 #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1397 static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
1398 				  const struct bpf_insn *insn) = {
1399 EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1400 EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1401 EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1402 };
1403 #undef PROG_NAME_LIST
1404 
1405 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
1406 {
1407 	stack_depth = max_t(u32, stack_depth, 1);
1408 	insn->off = (s16) insn->imm;
1409 	insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
1410 		__bpf_call_base_args;
1411 	insn->code = BPF_JMP | BPF_CALL_ARGS;
1412 }
1413 
1414 #else
1415 static unsigned int __bpf_prog_ret0_warn(const void *ctx,
1416 					 const struct bpf_insn *insn)
1417 {
1418 	/* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
1419 	 * is not working properly, so warn about it!
1420 	 */
1421 	WARN_ON_ONCE(1);
1422 	return 0;
1423 }
1424 #endif
1425 
1426 bool bpf_prog_array_compatible(struct bpf_array *array,
1427 			       const struct bpf_prog *fp)
1428 {
1429 	if (fp->kprobe_override)
1430 		return false;
1431 
1432 	if (!array->owner_prog_type) {
1433 		/* There's no owner yet where we could check for
1434 		 * compatibility.
1435 		 */
1436 		array->owner_prog_type = fp->type;
1437 		array->owner_jited = fp->jited;
1438 
1439 		return true;
1440 	}
1441 
1442 	return array->owner_prog_type == fp->type &&
1443 	       array->owner_jited == fp->jited;
1444 }
1445 
1446 static int bpf_check_tail_call(const struct bpf_prog *fp)
1447 {
1448 	struct bpf_prog_aux *aux = fp->aux;
1449 	int i;
1450 
1451 	for (i = 0; i < aux->used_map_cnt; i++) {
1452 		struct bpf_map *map = aux->used_maps[i];
1453 		struct bpf_array *array;
1454 
1455 		if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1456 			continue;
1457 
1458 		array = container_of(map, struct bpf_array, map);
1459 		if (!bpf_prog_array_compatible(array, fp))
1460 			return -EINVAL;
1461 	}
1462 
1463 	return 0;
1464 }
1465 
1466 /**
1467  *	bpf_prog_select_runtime - select exec runtime for BPF program
1468  *	@fp: bpf_prog populated with internal BPF program
1469  *	@err: pointer to error variable
1470  *
1471  * Try to JIT eBPF program, if JIT is not available, use interpreter.
1472  * The BPF program will be executed via BPF_PROG_RUN() macro.
1473  */
1474 struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
1475 {
1476 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1477 	u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1478 
1479 	fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
1480 #else
1481 	fp->bpf_func = __bpf_prog_ret0_warn;
1482 #endif
1483 
1484 	/* eBPF JITs can rewrite the program in case constant
1485 	 * blinding is active. However, in case of error during
1486 	 * blinding, bpf_int_jit_compile() must always return a
1487 	 * valid program, which in this case would simply not
1488 	 * be JITed, but falls back to the interpreter.
1489 	 */
1490 	if (!bpf_prog_is_dev_bound(fp->aux)) {
1491 		fp = bpf_int_jit_compile(fp);
1492 #ifdef CONFIG_BPF_JIT_ALWAYS_ON
1493 		if (!fp->jited) {
1494 			*err = -ENOTSUPP;
1495 			return fp;
1496 		}
1497 #endif
1498 	} else {
1499 		*err = bpf_prog_offload_compile(fp);
1500 		if (*err)
1501 			return fp;
1502 	}
1503 	bpf_prog_lock_ro(fp);
1504 
1505 	/* The tail call compatibility check can only be done at
1506 	 * this late stage as we need to determine, if we deal
1507 	 * with JITed or non JITed program concatenations and not
1508 	 * all eBPF JITs might immediately support all features.
1509 	 */
1510 	*err = bpf_check_tail_call(fp);
1511 
1512 	return fp;
1513 }
1514 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
1515 
1516 static unsigned int __bpf_prog_ret1(const void *ctx,
1517 				    const struct bpf_insn *insn)
1518 {
1519 	return 1;
1520 }
1521 
1522 static struct bpf_prog_dummy {
1523 	struct bpf_prog prog;
1524 } dummy_bpf_prog = {
1525 	.prog = {
1526 		.bpf_func = __bpf_prog_ret1,
1527 	},
1528 };
1529 
1530 /* to avoid allocating empty bpf_prog_array for cgroups that
1531  * don't have bpf program attached use one global 'empty_prog_array'
1532  * It will not be modified the caller of bpf_prog_array_alloc()
1533  * (since caller requested prog_cnt == 0)
1534  * that pointer should be 'freed' by bpf_prog_array_free()
1535  */
1536 static struct {
1537 	struct bpf_prog_array hdr;
1538 	struct bpf_prog *null_prog;
1539 } empty_prog_array = {
1540 	.null_prog = NULL,
1541 };
1542 
1543 struct bpf_prog_array __rcu *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
1544 {
1545 	if (prog_cnt)
1546 		return kzalloc(sizeof(struct bpf_prog_array) +
1547 			       sizeof(struct bpf_prog *) * (prog_cnt + 1),
1548 			       flags);
1549 
1550 	return &empty_prog_array.hdr;
1551 }
1552 
1553 void bpf_prog_array_free(struct bpf_prog_array __rcu *progs)
1554 {
1555 	if (!progs ||
1556 	    progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr)
1557 		return;
1558 	kfree_rcu(progs, rcu);
1559 }
1560 
1561 int bpf_prog_array_length(struct bpf_prog_array __rcu *progs)
1562 {
1563 	struct bpf_prog **prog;
1564 	u32 cnt = 0;
1565 
1566 	rcu_read_lock();
1567 	prog = rcu_dereference(progs)->progs;
1568 	for (; *prog; prog++)
1569 		if (*prog != &dummy_bpf_prog.prog)
1570 			cnt++;
1571 	rcu_read_unlock();
1572 	return cnt;
1573 }
1574 
1575 static bool bpf_prog_array_copy_core(struct bpf_prog **prog,
1576 				     u32 *prog_ids,
1577 				     u32 request_cnt)
1578 {
1579 	int i = 0;
1580 
1581 	for (; *prog; prog++) {
1582 		if (*prog == &dummy_bpf_prog.prog)
1583 			continue;
1584 		prog_ids[i] = (*prog)->aux->id;
1585 		if (++i == request_cnt) {
1586 			prog++;
1587 			break;
1588 		}
1589 	}
1590 
1591 	return !!(*prog);
1592 }
1593 
1594 int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs,
1595 				__u32 __user *prog_ids, u32 cnt)
1596 {
1597 	struct bpf_prog **prog;
1598 	unsigned long err = 0;
1599 	bool nospc;
1600 	u32 *ids;
1601 
1602 	/* users of this function are doing:
1603 	 * cnt = bpf_prog_array_length();
1604 	 * if (cnt > 0)
1605 	 *     bpf_prog_array_copy_to_user(..., cnt);
1606 	 * so below kcalloc doesn't need extra cnt > 0 check, but
1607 	 * bpf_prog_array_length() releases rcu lock and
1608 	 * prog array could have been swapped with empty or larger array,
1609 	 * so always copy 'cnt' prog_ids to the user.
1610 	 * In a rare race the user will see zero prog_ids
1611 	 */
1612 	ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN);
1613 	if (!ids)
1614 		return -ENOMEM;
1615 	rcu_read_lock();
1616 	prog = rcu_dereference(progs)->progs;
1617 	nospc = bpf_prog_array_copy_core(prog, ids, cnt);
1618 	rcu_read_unlock();
1619 	err = copy_to_user(prog_ids, ids, cnt * sizeof(u32));
1620 	kfree(ids);
1621 	if (err)
1622 		return -EFAULT;
1623 	if (nospc)
1624 		return -ENOSPC;
1625 	return 0;
1626 }
1627 
1628 void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs,
1629 				struct bpf_prog *old_prog)
1630 {
1631 	struct bpf_prog **prog = progs->progs;
1632 
1633 	for (; *prog; prog++)
1634 		if (*prog == old_prog) {
1635 			WRITE_ONCE(*prog, &dummy_bpf_prog.prog);
1636 			break;
1637 		}
1638 }
1639 
1640 int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
1641 			struct bpf_prog *exclude_prog,
1642 			struct bpf_prog *include_prog,
1643 			struct bpf_prog_array **new_array)
1644 {
1645 	int new_prog_cnt, carry_prog_cnt = 0;
1646 	struct bpf_prog **existing_prog;
1647 	struct bpf_prog_array *array;
1648 	int new_prog_idx = 0;
1649 
1650 	/* Figure out how many existing progs we need to carry over to
1651 	 * the new array.
1652 	 */
1653 	if (old_array) {
1654 		existing_prog = old_array->progs;
1655 		for (; *existing_prog; existing_prog++) {
1656 			if (*existing_prog != exclude_prog &&
1657 			    *existing_prog != &dummy_bpf_prog.prog)
1658 				carry_prog_cnt++;
1659 			if (*existing_prog == include_prog)
1660 				return -EEXIST;
1661 		}
1662 	}
1663 
1664 	/* How many progs (not NULL) will be in the new array? */
1665 	new_prog_cnt = carry_prog_cnt;
1666 	if (include_prog)
1667 		new_prog_cnt += 1;
1668 
1669 	/* Do we have any prog (not NULL) in the new array? */
1670 	if (!new_prog_cnt) {
1671 		*new_array = NULL;
1672 		return 0;
1673 	}
1674 
1675 	/* +1 as the end of prog_array is marked with NULL */
1676 	array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
1677 	if (!array)
1678 		return -ENOMEM;
1679 
1680 	/* Fill in the new prog array */
1681 	if (carry_prog_cnt) {
1682 		existing_prog = old_array->progs;
1683 		for (; *existing_prog; existing_prog++)
1684 			if (*existing_prog != exclude_prog &&
1685 			    *existing_prog != &dummy_bpf_prog.prog)
1686 				array->progs[new_prog_idx++] = *existing_prog;
1687 	}
1688 	if (include_prog)
1689 		array->progs[new_prog_idx++] = include_prog;
1690 	array->progs[new_prog_idx] = NULL;
1691 	*new_array = array;
1692 	return 0;
1693 }
1694 
1695 int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
1696 			     u32 *prog_ids, u32 request_cnt,
1697 			     u32 *prog_cnt)
1698 {
1699 	struct bpf_prog **prog;
1700 	u32 cnt = 0;
1701 
1702 	if (array)
1703 		cnt = bpf_prog_array_length(array);
1704 
1705 	*prog_cnt = cnt;
1706 
1707 	/* return early if user requested only program count or nothing to copy */
1708 	if (!request_cnt || !cnt)
1709 		return 0;
1710 
1711 	/* this function is called under trace/bpf_trace.c: bpf_event_mutex */
1712 	prog = rcu_dereference_check(array, 1)->progs;
1713 	return bpf_prog_array_copy_core(prog, prog_ids, request_cnt) ? -ENOSPC
1714 								     : 0;
1715 }
1716 
1717 static void bpf_prog_free_deferred(struct work_struct *work)
1718 {
1719 	struct bpf_prog_aux *aux;
1720 	int i;
1721 
1722 	aux = container_of(work, struct bpf_prog_aux, work);
1723 	if (bpf_prog_is_dev_bound(aux))
1724 		bpf_prog_offload_destroy(aux->prog);
1725 	for (i = 0; i < aux->func_cnt; i++)
1726 		bpf_jit_free(aux->func[i]);
1727 	if (aux->func_cnt) {
1728 		kfree(aux->func);
1729 		bpf_prog_unlock_free(aux->prog);
1730 	} else {
1731 		bpf_jit_free(aux->prog);
1732 	}
1733 }
1734 
1735 /* Free internal BPF program */
1736 void bpf_prog_free(struct bpf_prog *fp)
1737 {
1738 	struct bpf_prog_aux *aux = fp->aux;
1739 
1740 	INIT_WORK(&aux->work, bpf_prog_free_deferred);
1741 	schedule_work(&aux->work);
1742 }
1743 EXPORT_SYMBOL_GPL(bpf_prog_free);
1744 
1745 /* RNG for unpriviledged user space with separated state from prandom_u32(). */
1746 static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1747 
1748 void bpf_user_rnd_init_once(void)
1749 {
1750 	prandom_init_once(&bpf_user_rnd_state);
1751 }
1752 
1753 BPF_CALL_0(bpf_user_rnd_u32)
1754 {
1755 	/* Should someone ever have the rather unwise idea to use some
1756 	 * of the registers passed into this function, then note that
1757 	 * this function is called from native eBPF and classic-to-eBPF
1758 	 * transformations. Register assignments from both sides are
1759 	 * different, f.e. classic always sets fn(ctx, A, X) here.
1760 	 */
1761 	struct rnd_state *state;
1762 	u32 res;
1763 
1764 	state = &get_cpu_var(bpf_user_rnd_state);
1765 	res = prandom_u32_state(state);
1766 	put_cpu_var(bpf_user_rnd_state);
1767 
1768 	return res;
1769 }
1770 
1771 /* Weak definitions of helper functions in case we don't have bpf syscall. */
1772 const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1773 const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1774 const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1775 
1776 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
1777 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
1778 const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
1779 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
1780 
1781 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1782 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1783 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
1784 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
1785 
1786 const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1787 {
1788 	return NULL;
1789 }
1790 
1791 u64 __weak
1792 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1793 		 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
1794 {
1795 	return -ENOTSUPP;
1796 }
1797 
1798 /* Always built-in helper functions. */
1799 const struct bpf_func_proto bpf_tail_call_proto = {
1800 	.func		= NULL,
1801 	.gpl_only	= false,
1802 	.ret_type	= RET_VOID,
1803 	.arg1_type	= ARG_PTR_TO_CTX,
1804 	.arg2_type	= ARG_CONST_MAP_PTR,
1805 	.arg3_type	= ARG_ANYTHING,
1806 };
1807 
1808 /* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1809  * It is encouraged to implement bpf_int_jit_compile() instead, so that
1810  * eBPF and implicitly also cBPF can get JITed!
1811  */
1812 struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
1813 {
1814 	return prog;
1815 }
1816 
1817 /* Stub for JITs that support eBPF. All cBPF code gets transformed into
1818  * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1819  */
1820 void __weak bpf_jit_compile(struct bpf_prog *prog)
1821 {
1822 }
1823 
1824 bool __weak bpf_helper_changes_pkt_data(void *func)
1825 {
1826 	return false;
1827 }
1828 
1829 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1830  * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1831  */
1832 int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1833 			 int len)
1834 {
1835 	return -EFAULT;
1836 }
1837 
1838 /* All definitions of tracepoints related to BPF. */
1839 #define CREATE_TRACE_POINTS
1840 #include <linux/bpf_trace.h>
1841 
1842 EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
1843 
1844 /* These are only used within the BPF_SYSCALL code */
1845 #ifdef CONFIG_BPF_SYSCALL
1846 EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_get_type);
1847 EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_put_rcu);
1848 #endif
1849