Merge tag 'pci-v4.17-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[linux-2.6-block.git] / kernel / bpf / core.c
CommitLineData
f5bffecd
AS
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.
4df95ff4 21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
f5bffecd 22 */
738cbe72 23
f5bffecd
AS
24#include <linux/filter.h>
25#include <linux/skbuff.h>
60a3b225 26#include <linux/vmalloc.h>
738cbe72
DB
27#include <linux/random.h>
28#include <linux/moduleloader.h>
09756af4 29#include <linux/bpf.h>
39853cc0 30#include <linux/frame.h>
74451e66
DB
31#include <linux/rbtree_latch.h>
32#include <linux/kallsyms.h>
33#include <linux/rcupdate.h>
f5bffecd 34
3324b584
DB
35#include <asm/unaligned.h>
36
f5bffecd
AS
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 */
62void *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;
3324b584 70
f5bffecd
AS
71 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
72 return ptr;
73
74 return NULL;
75}
76
60a3b225
DB
77struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
78{
19809c2d 79 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
09756af4 80 struct bpf_prog_aux *aux;
60a3b225
DB
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
09756af4
AS
88 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
89 if (aux == NULL) {
60a3b225
DB
90 vfree(fp);
91 return NULL;
92 }
93
94 fp->pages = size / PAGE_SIZE;
09756af4 95 fp->aux = aux;
e9d8afa9 96 fp->aux->prog = fp;
60b58afc 97 fp->jit_requested = ebpf_jit_enabled();
60a3b225 98
74451e66
DB
99 INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode);
100
60a3b225
DB
101 return fp;
102}
103EXPORT_SYMBOL_GPL(bpf_prog_alloc);
104
105struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
106 gfp_t gfp_extra_flags)
107{
19809c2d 108 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
60a3b225 109 struct bpf_prog *fp;
5ccb071e
DB
110 u32 pages, delta;
111 int ret;
60a3b225
DB
112
113 BUG_ON(fp_old == NULL);
114
115 size = round_up(size, PAGE_SIZE);
5ccb071e
DB
116 pages = size / PAGE_SIZE;
117 if (pages <= fp_old->pages)
60a3b225
DB
118 return fp_old;
119
5ccb071e
DB
120 delta = pages - fp_old->pages;
121 ret = __bpf_prog_charge(fp_old->aux->user, delta);
122 if (ret)
123 return NULL;
124
60a3b225 125 fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
5ccb071e
DB
126 if (fp == NULL) {
127 __bpf_prog_uncharge(fp_old->aux->user, delta);
128 } else {
60a3b225 129 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
5ccb071e 130 fp->pages = pages;
e9d8afa9 131 fp->aux->prog = fp;
60a3b225 132
09756af4 133 /* We keep fp->aux from fp_old around in the new
60a3b225
DB
134 * reallocated structure.
135 */
09756af4 136 fp_old->aux = NULL;
60a3b225
DB
137 __bpf_prog_free(fp_old);
138 }
139
140 return fp;
141}
60a3b225
DB
142
143void __bpf_prog_free(struct bpf_prog *fp)
144{
09756af4 145 kfree(fp->aux);
60a3b225
DB
146 vfree(fp);
147}
60a3b225 148
f1f7714e 149int bpf_prog_calc_tag(struct bpf_prog *fp)
7bd509e3
DB
150{
151 const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64);
f1f7714e
DB
152 u32 raw_size = bpf_prog_tag_scratch_size(fp);
153 u32 digest[SHA_DIGEST_WORDS];
aafe6ae9 154 u32 ws[SHA_WORKSPACE_WORDS];
7bd509e3 155 u32 i, bsize, psize, blocks;
aafe6ae9 156 struct bpf_insn *dst;
7bd509e3 157 bool was_ld_map;
aafe6ae9 158 u8 *raw, *todo;
7bd509e3
DB
159 __be32 *result;
160 __be64 *bits;
161
aafe6ae9
DB
162 raw = vmalloc(raw_size);
163 if (!raw)
164 return -ENOMEM;
165
f1f7714e 166 sha_init(digest);
7bd509e3
DB
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 */
aafe6ae9 172 dst = (void *)raw;
7bd509e3
DB
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
aafe6ae9
DB
192 psize = bpf_prog_insn_size(fp);
193 memset(&raw[psize], 0, raw_size - psize);
7bd509e3
DB
194 raw[psize++] = 0x80;
195
196 bsize = round_up(psize, SHA_MESSAGE_BYTES);
197 blocks = bsize / SHA_MESSAGE_BYTES;
aafe6ae9 198 todo = raw;
7bd509e3
DB
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--) {
f1f7714e 208 sha_transform(digest, todo, ws);
7bd509e3
DB
209 todo += SHA_MESSAGE_BYTES;
210 }
211
f1f7714e 212 result = (__force __be32 *)digest;
7bd509e3 213 for (i = 0; i < SHA_DIGEST_WORDS; i++)
f1f7714e
DB
214 result[i] = cpu_to_be32(digest[i]);
215 memcpy(fp->tag, result, sizeof(fp->tag));
aafe6ae9
DB
216
217 vfree(raw);
218 return 0;
7bd509e3
DB
219}
220
c237ee5e
DB
221static 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;
1ea47e01
AS
225 bool pseudo_call;
226 u8 code;
227 int off;
c237ee5e
DB
228
229 for (i = 0; i < insn_cnt; i++, insn++) {
1ea47e01
AS
230 code = insn->code;
231 if (BPF_CLASS(code) != BPF_JMP)
c237ee5e 232 continue;
1ea47e01
AS
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;
c237ee5e
DB
244
245 /* Adjust offset of jmps if we cross boundaries. */
1ea47e01
AS
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;
c237ee5e
DB
255 }
256}
257
258struct 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
b954d834 302#ifdef CONFIG_BPF_JIT
fa9dd599
DB
303/* All BPF JIT sysctl knobs here. */
304int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
305int bpf_jit_harden __read_mostly;
306int bpf_jit_kallsyms __read_mostly;
307
74451e66
DB
308static __always_inline void
309bpf_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
322static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
323{
368211fb
MKL
324 const char *end = sym + KSYM_NAME_LEN;
325
74451e66 326 BUILD_BUG_ON(sizeof("bpf_prog_") +
368211fb
MKL
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);
74451e66
DB
336
337 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
338 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
368211fb
MKL
339 if (prog->aux->name[0])
340 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
341 else
342 *sym = 0;
74451e66
DB
343}
344
345static __always_inline unsigned long
346bpf_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
357static __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
363static __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
380static const struct latch_tree_ops bpf_tree_ops = {
381 .less = bpf_tree_less,
382 .comp = bpf_tree_comp,
383};
384
385static DEFINE_SPINLOCK(bpf_lock);
386static LIST_HEAD(bpf_kallsyms);
387static struct latch_tree_root bpf_tree __cacheline_aligned;
388
74451e66
DB
389static 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
396static 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
405static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
406{
407 return fp->jited && !bpf_prog_was_classic(fp);
408}
409
410static 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
416void bpf_prog_kallsyms_add(struct bpf_prog *fp)
417{
74451e66
DB
418 if (!bpf_prog_kallsyms_candidate(fp) ||
419 !capable(CAP_SYS_ADMIN))
420 return;
421
d24f7c7f 422 spin_lock_bh(&bpf_lock);
74451e66 423 bpf_prog_ksym_node_add(fp->aux);
d24f7c7f 424 spin_unlock_bh(&bpf_lock);
74451e66
DB
425}
426
427void bpf_prog_kallsyms_del(struct bpf_prog *fp)
428{
74451e66
DB
429 if (!bpf_prog_kallsyms_candidate(fp))
430 return;
431
d24f7c7f 432 spin_lock_bh(&bpf_lock);
74451e66 433 bpf_prog_ksym_node_del(fp->aux);
d24f7c7f 434 spin_unlock_bh(&bpf_lock);
74451e66
DB
435}
436
437static 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
450const 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
474bool 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
485int 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
738cbe72
DB
515struct bpf_binary_header *
516bpf_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));
b7552e1b 538 start = (get_random_int() % hole) & ~(alignment - 1);
738cbe72
DB
539
540 /* Leave a random number of instructions before BPF code. */
541 *image_ptr = &hdr->image[start];
542
543 return hdr;
544}
545
546void bpf_jit_binary_free(struct bpf_binary_header *hdr)
547{
be1f221c 548 module_memfree(hdr);
738cbe72 549}
4f3446bb 550
74451e66
DB
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 */
555void __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
4f3446bb
DB
569static 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;
b7552e1b 574 u32 imm_rnd = get_random_int();
4f3446bb
DB
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:
92b31a9a 619 case BPF_JMP | BPF_JLT | BPF_K:
4f3446bb 620 case BPF_JMP | BPF_JGE | BPF_K:
92b31a9a 621 case BPF_JMP | BPF_JLE | BPF_K:
4f3446bb 622 case BPF_JMP | BPF_JSGT | BPF_K:
92b31a9a 623 case BPF_JMP | BPF_JSLT | BPF_K:
4f3446bb 624 case BPF_JMP | BPF_JSGE | BPF_K:
92b31a9a 625 case BPF_JMP | BPF_JSLE | BPF_K:
4f3446bb
DB
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 }
674out:
675 return to - to_buff;
676}
677
678static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
679 gfp_t gfp_extra_flags)
680{
19809c2d 681 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
4f3446bb
DB
682 struct bpf_prog *fp;
683
684 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
685 if (fp != NULL) {
4f3446bb
DB
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
696static 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
709void 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
718struct 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
1c2a088a 726 if (!bpf_jit_blinding_enabled(prog) || prog->blinded)
4f3446bb
DB
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
1c2a088a 768 clone->blinded = 1;
4f3446bb
DB
769 return clone;
770}
b954d834 771#endif /* CONFIG_BPF_JIT */
738cbe72 772
f5bffecd
AS
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
7105e828
DB
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.
f5bffecd
AS
778 */
779noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
780{
781 return 0;
782}
4d9c5c53 783EXPORT_SYMBOL_GPL(__bpf_call_base);
f5bffecd 784
5e581dad
DB
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
902bool 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
290af866 916#ifndef CONFIG_BPF_JIT_ALWAYS_ON
f5bffecd 917/**
7ae457c1
AS
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
f5bffecd 921 *
7ae457c1 922 * Decode and execute eBPF instructions.
f5bffecd 923 */
1ea47e01 924static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
f5bffecd 925{
f696b8f4 926 u64 tmp;
5e581dad
DB
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
f5bffecd
AS
929 static const void *jumptable[256] = {
930 [0 ... 255] = &&default_label,
931 /* Now overwrite non-defaults ... */
5e581dad
DB
932 BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL),
933 /* Non-UAPI available opcodes. */
1ea47e01 934 [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
71189fa9 935 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
f5bffecd 936 };
5e581dad
DB
937#undef BPF_INSN_3_LBL
938#undef BPF_INSN_2_LBL
04fd61ab 939 u32 tail_call_cnt = 0;
f5bffecd
AS
940 void *ptr;
941 int off;
942
943#define CONT ({ insn++; goto select_insn; })
944#define CONT_JMP ({ insn++; goto select_insn; })
945
f5bffecd
AS
946select_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;
02ab695b
AS
991 LD_IMM_DW:
992 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
993 insn++;
994 CONT;
f5bffecd
AS
995 ALU64_ARSH_X:
996 (*(s64 *) &DST) >>= SRC;
997 CONT;
998 ALU64_ARSH_K:
999 (*(s64 *) &DST) >>= IMM;
1000 CONT;
1001 ALU64_MOD_X:
876a7ae6
AS
1002 div64_u64_rem(DST, SRC, &tmp);
1003 DST = tmp;
f5bffecd
AS
1004 CONT;
1005 ALU_MOD_X:
f5bffecd
AS
1006 tmp = (u32) DST;
1007 DST = do_div(tmp, (u32) SRC);
1008 CONT;
1009 ALU64_MOD_K:
876a7ae6
AS
1010 div64_u64_rem(DST, IMM, &tmp);
1011 DST = tmp;
f5bffecd
AS
1012 CONT;
1013 ALU_MOD_K:
1014 tmp = (u32) DST;
1015 DST = do_div(tmp, (u32) IMM);
1016 CONT;
1017 ALU64_DIV_X:
876a7ae6 1018 DST = div64_u64(DST, SRC);
f5bffecd
AS
1019 CONT;
1020 ALU_DIV_X:
f5bffecd
AS
1021 tmp = (u32) DST;
1022 do_div(tmp, (u32) SRC);
1023 DST = (u32) tmp;
1024 CONT;
1025 ALU64_DIV_K:
876a7ae6 1026 DST = div64_u64(DST, IMM);
f5bffecd
AS
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
1ea47e01
AS
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
04fd61ab
AS
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;
90caccdd 1081 u32 index = BPF_R3;
04fd61ab
AS
1082
1083 if (unlikely(index >= array->map.max_entries))
1084 goto out;
04fd61ab
AS
1085 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1086 goto out;
1087
1088 tail_call_cnt++;
1089
2a36f0b9 1090 prog = READ_ONCE(array->ptrs[index]);
1ca1cc98 1091 if (!prog)
04fd61ab
AS
1092 goto out;
1093
c4675f93
DB
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 */
04fd61ab
AS
1099 insn = prog->insnsi;
1100 goto select_insn;
1101out:
1102 CONT;
1103 }
f5bffecd
AS
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;
92b31a9a
DB
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;
f5bffecd
AS
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;
92b31a9a
DB
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;
f5bffecd
AS
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;
92b31a9a
DB
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;
f5bffecd
AS
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;
92b31a9a
DB
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;
f5bffecd
AS
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;
1270load_word:
96a94cc5
JB
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.
f5bffecd
AS
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;
1302load_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;
1312load_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:
5e581dad
DB
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);
f5bffecd
AS
1339 return 0;
1340}
f696b8f4
AS
1341STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1342
b870aa90
AS
1343#define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1344#define DEFINE_BPF_PROG_RUN(stack_size) \
1345static 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); \
f696b8f4 1353}
f5bffecd 1354
1ea47e01
AS
1355#define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
1356#define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
1357static 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
b870aa90
AS
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
1379EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1380EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1381EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1382
1ea47e01
AS
1383EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
1384EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
1385EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
1386
b870aa90
AS
1387#define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1388
1389static unsigned int (*interpreters[])(const void *ctx,
1390 const struct bpf_insn *insn) = {
1391EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1392EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1393EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1394};
1ea47e01
AS
1395#undef PROG_NAME_LIST
1396#define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
1397static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
1398 const struct bpf_insn *insn) = {
1399EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1400EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1401EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1402};
1403#undef PROG_NAME_LIST
1404
1405void 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}
b870aa90 1413
290af866 1414#else
fa9dd599
DB
1415static unsigned int __bpf_prog_ret0_warn(const void *ctx,
1416 const struct bpf_insn *insn)
290af866 1417{
fa9dd599
DB
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);
290af866
AS
1422 return 0;
1423}
1424#endif
1425
3324b584
DB
1426bool bpf_prog_array_compatible(struct bpf_array *array,
1427 const struct bpf_prog *fp)
04fd61ab 1428{
9802d865
JB
1429 if (fp->kprobe_override)
1430 return false;
1431
3324b584
DB
1432 if (!array->owner_prog_type) {
1433 /* There's no owner yet where we could check for
1434 * compatibility.
1435 */
04fd61ab
AS
1436 array->owner_prog_type = fp->type;
1437 array->owner_jited = fp->jited;
3324b584
DB
1438
1439 return true;
04fd61ab 1440 }
3324b584
DB
1441
1442 return array->owner_prog_type == fp->type &&
1443 array->owner_jited == fp->jited;
04fd61ab
AS
1444}
1445
3324b584 1446static int bpf_check_tail_call(const struct bpf_prog *fp)
04fd61ab
AS
1447{
1448 struct bpf_prog_aux *aux = fp->aux;
1449 int i;
1450
1451 for (i = 0; i < aux->used_map_cnt; i++) {
3324b584 1452 struct bpf_map *map = aux->used_maps[i];
04fd61ab 1453 struct bpf_array *array;
04fd61ab 1454
04fd61ab
AS
1455 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1456 continue;
3324b584 1457
04fd61ab
AS
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
f5bffecd 1466/**
3324b584 1467 * bpf_prog_select_runtime - select exec runtime for BPF program
7ae457c1 1468 * @fp: bpf_prog populated with internal BPF program
d1c55ab5 1469 * @err: pointer to error variable
f5bffecd 1470 *
3324b584
DB
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.
f5bffecd 1473 */
d1c55ab5 1474struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
f5bffecd 1475{
290af866 1476#ifndef CONFIG_BPF_JIT_ALWAYS_ON
8007e40a
MKL
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];
290af866 1480#else
fa9dd599 1481 fp->bpf_func = __bpf_prog_ret0_warn;
290af866 1482#endif
f5bffecd 1483
d1c55ab5
DB
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 */
ab3f0063
JK
1490 if (!bpf_prog_is_dev_bound(fp->aux)) {
1491 fp = bpf_int_jit_compile(fp);
290af866
AS
1492#ifdef CONFIG_BPF_JIT_ALWAYS_ON
1493 if (!fp->jited) {
1494 *err = -ENOTSUPP;
1495 return fp;
1496 }
1497#endif
ab3f0063
JK
1498 } else {
1499 *err = bpf_prog_offload_compile(fp);
1500 if (*err)
1501 return fp;
1502 }
60a3b225 1503 bpf_prog_lock_ro(fp);
04fd61ab 1504
3324b584
DB
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 */
d1c55ab5
DB
1510 *err = bpf_check_tail_call(fp);
1511
1512 return fp;
f5bffecd 1513}
7ae457c1 1514EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
f5bffecd 1515
e87c6bc3
YS
1516static unsigned int __bpf_prog_ret1(const void *ctx,
1517 const struct bpf_insn *insn)
1518{
1519 return 1;
1520}
1521
1522static struct bpf_prog_dummy {
1523 struct bpf_prog prog;
1524} dummy_bpf_prog = {
1525 .prog = {
1526 .bpf_func = __bpf_prog_ret1,
1527 },
1528};
1529
324bda9e
AS
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 */
1536static struct {
1537 struct bpf_prog_array hdr;
1538 struct bpf_prog *null_prog;
1539} empty_prog_array = {
1540 .null_prog = NULL,
1541};
1542
1543struct 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
1553void 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
468e2f64
AS
1561int 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++)
c8c088ba
YS
1569 if (*prog != &dummy_bpf_prog.prog)
1570 cnt++;
468e2f64
AS
1571 rcu_read_unlock();
1572 return cnt;
1573}
1574
1575int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs,
1576 __u32 __user *prog_ids, u32 cnt)
1577{
1578 struct bpf_prog **prog;
0911287c
AS
1579 unsigned long err = 0;
1580 u32 i = 0, *ids;
1581 bool nospc;
1582
1583 /* users of this function are doing:
1584 * cnt = bpf_prog_array_length();
1585 * if (cnt > 0)
1586 * bpf_prog_array_copy_to_user(..., cnt);
1587 * so below kcalloc doesn't need extra cnt > 0 check, but
1588 * bpf_prog_array_length() releases rcu lock and
1589 * prog array could have been swapped with empty or larger array,
1590 * so always copy 'cnt' prog_ids to the user.
1591 * In a rare race the user will see zero prog_ids
1592 */
9c481b90 1593 ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN);
0911287c
AS
1594 if (!ids)
1595 return -ENOMEM;
468e2f64
AS
1596 rcu_read_lock();
1597 prog = rcu_dereference(progs)->progs;
1598 for (; *prog; prog++) {
f371b304
YS
1599 if (*prog == &dummy_bpf_prog.prog)
1600 continue;
0911287c 1601 ids[i] = (*prog)->aux->id;
468e2f64
AS
1602 if (++i == cnt) {
1603 prog++;
1604 break;
1605 }
1606 }
0911287c 1607 nospc = !!(*prog);
468e2f64 1608 rcu_read_unlock();
0911287c
AS
1609 err = copy_to_user(prog_ids, ids, cnt * sizeof(u32));
1610 kfree(ids);
1611 if (err)
1612 return -EFAULT;
1613 if (nospc)
468e2f64
AS
1614 return -ENOSPC;
1615 return 0;
1616}
1617
e87c6bc3
YS
1618void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs,
1619 struct bpf_prog *old_prog)
1620{
1621 struct bpf_prog **prog = progs->progs;
1622
1623 for (; *prog; prog++)
1624 if (*prog == old_prog) {
1625 WRITE_ONCE(*prog, &dummy_bpf_prog.prog);
1626 break;
1627 }
1628}
1629
1630int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
1631 struct bpf_prog *exclude_prog,
1632 struct bpf_prog *include_prog,
1633 struct bpf_prog_array **new_array)
1634{
1635 int new_prog_cnt, carry_prog_cnt = 0;
1636 struct bpf_prog **existing_prog;
1637 struct bpf_prog_array *array;
1638 int new_prog_idx = 0;
1639
1640 /* Figure out how many existing progs we need to carry over to
1641 * the new array.
1642 */
1643 if (old_array) {
1644 existing_prog = old_array->progs;
1645 for (; *existing_prog; existing_prog++) {
1646 if (*existing_prog != exclude_prog &&
1647 *existing_prog != &dummy_bpf_prog.prog)
1648 carry_prog_cnt++;
1649 if (*existing_prog == include_prog)
1650 return -EEXIST;
1651 }
1652 }
1653
1654 /* How many progs (not NULL) will be in the new array? */
1655 new_prog_cnt = carry_prog_cnt;
1656 if (include_prog)
1657 new_prog_cnt += 1;
1658
1659 /* Do we have any prog (not NULL) in the new array? */
1660 if (!new_prog_cnt) {
1661 *new_array = NULL;
1662 return 0;
1663 }
1664
1665 /* +1 as the end of prog_array is marked with NULL */
1666 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
1667 if (!array)
1668 return -ENOMEM;
1669
1670 /* Fill in the new prog array */
1671 if (carry_prog_cnt) {
1672 existing_prog = old_array->progs;
1673 for (; *existing_prog; existing_prog++)
1674 if (*existing_prog != exclude_prog &&
1675 *existing_prog != &dummy_bpf_prog.prog)
1676 array->progs[new_prog_idx++] = *existing_prog;
1677 }
1678 if (include_prog)
1679 array->progs[new_prog_idx++] = include_prog;
1680 array->progs[new_prog_idx] = NULL;
1681 *new_array = array;
1682 return 0;
1683}
1684
f371b304
YS
1685int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
1686 __u32 __user *prog_ids, u32 request_cnt,
1687 __u32 __user *prog_cnt)
1688{
1689 u32 cnt = 0;
1690
1691 if (array)
1692 cnt = bpf_prog_array_length(array);
1693
1694 if (copy_to_user(prog_cnt, &cnt, sizeof(cnt)))
1695 return -EFAULT;
1696
1697 /* return early if user requested only program count or nothing to copy */
1698 if (!request_cnt || !cnt)
1699 return 0;
1700
1701 return bpf_prog_array_copy_to_user(array, prog_ids, request_cnt);
1702}
1703
60a3b225
DB
1704static void bpf_prog_free_deferred(struct work_struct *work)
1705{
09756af4 1706 struct bpf_prog_aux *aux;
1c2a088a 1707 int i;
60a3b225 1708
09756af4 1709 aux = container_of(work, struct bpf_prog_aux, work);
ab3f0063
JK
1710 if (bpf_prog_is_dev_bound(aux))
1711 bpf_prog_offload_destroy(aux->prog);
1c2a088a
AS
1712 for (i = 0; i < aux->func_cnt; i++)
1713 bpf_jit_free(aux->func[i]);
1714 if (aux->func_cnt) {
1715 kfree(aux->func);
1716 bpf_prog_unlock_free(aux->prog);
1717 } else {
1718 bpf_jit_free(aux->prog);
1719 }
60a3b225
DB
1720}
1721
1722/* Free internal BPF program */
7ae457c1 1723void bpf_prog_free(struct bpf_prog *fp)
f5bffecd 1724{
09756af4 1725 struct bpf_prog_aux *aux = fp->aux;
60a3b225 1726
09756af4 1727 INIT_WORK(&aux->work, bpf_prog_free_deferred);
09756af4 1728 schedule_work(&aux->work);
f5bffecd 1729}
7ae457c1 1730EXPORT_SYMBOL_GPL(bpf_prog_free);
f89b7755 1731
3ad00405
DB
1732/* RNG for unpriviledged user space with separated state from prandom_u32(). */
1733static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1734
1735void bpf_user_rnd_init_once(void)
1736{
1737 prandom_init_once(&bpf_user_rnd_state);
1738}
1739
f3694e00 1740BPF_CALL_0(bpf_user_rnd_u32)
3ad00405
DB
1741{
1742 /* Should someone ever have the rather unwise idea to use some
1743 * of the registers passed into this function, then note that
1744 * this function is called from native eBPF and classic-to-eBPF
1745 * transformations. Register assignments from both sides are
1746 * different, f.e. classic always sets fn(ctx, A, X) here.
1747 */
1748 struct rnd_state *state;
1749 u32 res;
1750
1751 state = &get_cpu_var(bpf_user_rnd_state);
1752 res = prandom_u32_state(state);
b761fe22 1753 put_cpu_var(bpf_user_rnd_state);
3ad00405
DB
1754
1755 return res;
1756}
1757
3ba67dab
DB
1758/* Weak definitions of helper functions in case we don't have bpf syscall. */
1759const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1760const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1761const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1762
03e69b50 1763const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
c04167ce 1764const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
2d0e30c3 1765const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
17ca8cbf 1766const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
bd570ff9 1767
ffeedafb
AS
1768const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1769const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1770const struct bpf_func_proto bpf_get_current_comm_proto __weak;
6bdc9c4c 1771const struct bpf_func_proto bpf_sock_map_update_proto __weak;
bd570ff9 1772
0756ea3e
AS
1773const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1774{
1775 return NULL;
1776}
03e69b50 1777
555c8a86
DB
1778u64 __weak
1779bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1780 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
bd570ff9 1781{
555c8a86 1782 return -ENOTSUPP;
bd570ff9
DB
1783}
1784
3324b584
DB
1785/* Always built-in helper functions. */
1786const struct bpf_func_proto bpf_tail_call_proto = {
1787 .func = NULL,
1788 .gpl_only = false,
1789 .ret_type = RET_VOID,
1790 .arg1_type = ARG_PTR_TO_CTX,
1791 .arg2_type = ARG_CONST_MAP_PTR,
1792 .arg3_type = ARG_ANYTHING,
1793};
1794
9383191d
DB
1795/* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1796 * It is encouraged to implement bpf_int_jit_compile() instead, so that
1797 * eBPF and implicitly also cBPF can get JITed!
1798 */
d1c55ab5 1799struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
3324b584 1800{
d1c55ab5 1801 return prog;
3324b584
DB
1802}
1803
9383191d
DB
1804/* Stub for JITs that support eBPF. All cBPF code gets transformed into
1805 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1806 */
1807void __weak bpf_jit_compile(struct bpf_prog *prog)
1808{
1809}
1810
17bedab2 1811bool __weak bpf_helper_changes_pkt_data(void *func)
969bf05e
AS
1812{
1813 return false;
1814}
1815
f89b7755
AS
1816/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1817 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1818 */
1819int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1820 int len)
1821{
1822 return -EFAULT;
1823}
a67edbf4
DB
1824
1825/* All definitions of tracepoints related to BPF. */
1826#define CREATE_TRACE_POINTS
1827#include <linux/bpf_trace.h>
1828
1829EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
1830
9185a610
SRV
1831/* These are only used within the BPF_SYSCALL code */
1832#ifdef CONFIG_BPF_SYSCALL
a67edbf4
DB
1833EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_get_type);
1834EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_put_rcu);
9185a610 1835#endif