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