]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/bpf/core.c
btrfs: print message when tree-log replay starts
[mirror_ubuntu-bionic-kernel.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]
1ead6876 54#define AX regs[BPF_REG_AX]
f5bffecd
AS
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 */
63void *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;
3324b584 71
f5bffecd
AS
72 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
73 return ptr;
74
75 return NULL;
76}
77
60a3b225
DB
78struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
79{
19809c2d 80 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
09756af4 81 struct bpf_prog_aux *aux;
60a3b225
DB
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
09756af4
AS
89 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
90 if (aux == NULL) {
60a3b225
DB
91 vfree(fp);
92 return NULL;
93 }
94
95 fp->pages = size / PAGE_SIZE;
09756af4 96 fp->aux = aux;
e9d8afa9 97 fp->aux->prog = fp;
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 bool bpf_is_jmp_and_has_target(const struct bpf_insn *insn)
222{
223 return BPF_CLASS(insn->code) == BPF_JMP &&
224 /* Call and Exit are both special jumps with no
225 * target inside the BPF instruction image.
226 */
227 BPF_OP(insn->code) != BPF_CALL &&
228 BPF_OP(insn->code) != BPF_EXIT;
229}
230
231static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
232{
233 struct bpf_insn *insn = prog->insnsi;
234 u32 i, insn_cnt = prog->len;
235
236 for (i = 0; i < insn_cnt; i++, insn++) {
237 if (!bpf_is_jmp_and_has_target(insn))
238 continue;
239
240 /* Adjust offset of jmps if we cross boundaries. */
241 if (i < pos && i + insn->off + 1 > pos)
242 insn->off += delta;
243 else if (i > pos + delta && i + insn->off + 1 <= pos + delta)
244 insn->off -= delta;
245 }
246}
247
248struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
249 const struct bpf_insn *patch, u32 len)
250{
251 u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
252 struct bpf_prog *prog_adj;
253
254 /* Since our patchlet doesn't expand the image, we're done. */
255 if (insn_delta == 0) {
256 memcpy(prog->insnsi + off, patch, sizeof(*patch));
257 return prog;
258 }
259
260 insn_adj_cnt = prog->len + insn_delta;
261
262 /* Several new instructions need to be inserted. Make room
263 * for them. Likely, there's no need for a new allocation as
264 * last page could have large enough tailroom.
265 */
266 prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
267 GFP_USER);
268 if (!prog_adj)
269 return NULL;
270
271 prog_adj->len = insn_adj_cnt;
272
273 /* Patching happens in 3 steps:
274 *
275 * 1) Move over tail of insnsi from next instruction onwards,
276 * so we can patch the single target insn with one or more
277 * new ones (patching is always from 1 to n insns, n > 0).
278 * 2) Inject new instructions at the target location.
279 * 3) Adjust branch offsets if necessary.
280 */
281 insn_rest = insn_adj_cnt - off - len;
282
283 memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
284 sizeof(*patch) * insn_rest);
285 memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
286
287 bpf_adj_branches(prog_adj, off, insn_delta);
288
289 return prog_adj;
290}
291
b954d834 292#ifdef CONFIG_BPF_JIT
69e97d45
DB
293/* All BPF JIT sysctl knobs here. */
294int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_ALWAYS_ON);
295int bpf_jit_harden __read_mostly;
296int bpf_jit_kallsyms __read_mostly;
ce873e76 297long bpf_jit_limit __read_mostly;
69e97d45 298
74451e66
DB
299static __always_inline void
300bpf_get_prog_addr_region(const struct bpf_prog *prog,
301 unsigned long *symbol_start,
302 unsigned long *symbol_end)
303{
304 const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
305 unsigned long addr = (unsigned long)hdr;
306
307 WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
308
309 *symbol_start = addr;
310 *symbol_end = addr + hdr->pages * PAGE_SIZE;
311}
312
313static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
314{
368211fb
MKL
315 const char *end = sym + KSYM_NAME_LEN;
316
74451e66 317 BUILD_BUG_ON(sizeof("bpf_prog_") +
368211fb
MKL
318 sizeof(prog->tag) * 2 +
319 /* name has been null terminated.
320 * We should need +1 for the '_' preceding
321 * the name. However, the null character
322 * is double counted between the name and the
323 * sizeof("bpf_prog_") above, so we omit
324 * the +1 here.
325 */
326 sizeof(prog->aux->name) > KSYM_NAME_LEN);
74451e66
DB
327
328 sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
329 sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
368211fb
MKL
330 if (prog->aux->name[0])
331 snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
332 else
333 *sym = 0;
74451e66
DB
334}
335
336static __always_inline unsigned long
337bpf_get_prog_addr_start(struct latch_tree_node *n)
338{
339 unsigned long symbol_start, symbol_end;
340 const struct bpf_prog_aux *aux;
341
342 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
343 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
344
345 return symbol_start;
346}
347
348static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
349 struct latch_tree_node *b)
350{
351 return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
352}
353
354static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
355{
356 unsigned long val = (unsigned long)key;
357 unsigned long symbol_start, symbol_end;
358 const struct bpf_prog_aux *aux;
359
360 aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
361 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
362
363 if (val < symbol_start)
364 return -1;
365 if (val >= symbol_end)
366 return 1;
367
368 return 0;
369}
370
371static const struct latch_tree_ops bpf_tree_ops = {
372 .less = bpf_tree_less,
373 .comp = bpf_tree_comp,
374};
375
376static DEFINE_SPINLOCK(bpf_lock);
377static LIST_HEAD(bpf_kallsyms);
378static struct latch_tree_root bpf_tree __cacheline_aligned;
379
74451e66
DB
380static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
381{
382 WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
383 list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
384 latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
385}
386
387static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
388{
389 if (list_empty(&aux->ksym_lnode))
390 return;
391
392 latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
393 list_del_rcu(&aux->ksym_lnode);
394}
395
396static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
397{
398 return fp->jited && !bpf_prog_was_classic(fp);
399}
400
401static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
402{
403 return list_empty(&fp->aux->ksym_lnode) ||
404 fp->aux->ksym_lnode.prev == LIST_POISON2;
405}
406
407void bpf_prog_kallsyms_add(struct bpf_prog *fp)
408{
74451e66
DB
409 if (!bpf_prog_kallsyms_candidate(fp) ||
410 !capable(CAP_SYS_ADMIN))
411 return;
412
d24f7c7f 413 spin_lock_bh(&bpf_lock);
74451e66 414 bpf_prog_ksym_node_add(fp->aux);
d24f7c7f 415 spin_unlock_bh(&bpf_lock);
74451e66
DB
416}
417
418void bpf_prog_kallsyms_del(struct bpf_prog *fp)
419{
74451e66
DB
420 if (!bpf_prog_kallsyms_candidate(fp))
421 return;
422
d24f7c7f 423 spin_lock_bh(&bpf_lock);
74451e66 424 bpf_prog_ksym_node_del(fp->aux);
d24f7c7f 425 spin_unlock_bh(&bpf_lock);
74451e66
DB
426}
427
428static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
429{
430 struct latch_tree_node *n;
431
432 if (!bpf_jit_kallsyms_enabled())
433 return NULL;
434
435 n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
436 return n ?
437 container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
438 NULL;
439}
440
441const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
442 unsigned long *off, char *sym)
443{
444 unsigned long symbol_start, symbol_end;
445 struct bpf_prog *prog;
446 char *ret = NULL;
447
448 rcu_read_lock();
449 prog = bpf_prog_kallsyms_find(addr);
450 if (prog) {
451 bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
452 bpf_get_prog_name(prog, sym);
453
454 ret = sym;
455 if (size)
456 *size = symbol_end - symbol_start;
457 if (off)
458 *off = addr - symbol_start;
459 }
460 rcu_read_unlock();
461
462 return ret;
463}
464
465bool is_bpf_text_address(unsigned long addr)
466{
467 bool ret;
468
469 rcu_read_lock();
470 ret = bpf_prog_kallsyms_find(addr) != NULL;
471 rcu_read_unlock();
472
473 return ret;
474}
475
476int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
477 char *sym)
478{
479 unsigned long symbol_start, symbol_end;
480 struct bpf_prog_aux *aux;
481 unsigned int it = 0;
482 int ret = -ERANGE;
483
484 if (!bpf_jit_kallsyms_enabled())
485 return ret;
486
487 rcu_read_lock();
488 list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
489 if (it++ != symnum)
490 continue;
491
492 bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
493 bpf_get_prog_name(aux->prog, sym);
494
495 *value = symbol_start;
496 *type = BPF_SYM_ELF_TYPE;
497
498 ret = 0;
499 break;
500 }
501 rcu_read_unlock();
502
503 return ret;
504}
505
409a37eb
DB
506static atomic_long_t bpf_jit_current;
507
ce873e76
DB
508/* Can be overridden by an arch's JIT compiler if it has a custom,
509 * dedicated BPF backend memory area, or if neither of the two
510 * below apply.
511 */
512u64 __weak bpf_jit_alloc_exec_limit(void)
513{
409a37eb 514#if defined(MODULES_VADDR)
ce873e76
DB
515 return MODULES_END - MODULES_VADDR;
516#else
517 return VMALLOC_END - VMALLOC_START;
518#endif
519}
520
409a37eb
DB
521static int __init bpf_jit_charge_init(void)
522{
523 /* Only used as heuristic here to derive limit. */
ce873e76
DB
524 bpf_jit_limit = min_t(u64, round_up(bpf_jit_alloc_exec_limit() >> 2,
525 PAGE_SIZE), LONG_MAX);
409a37eb
DB
526 return 0;
527}
528pure_initcall(bpf_jit_charge_init);
409a37eb
DB
529
530static int bpf_jit_charge_modmem(u32 pages)
531{
532 if (atomic_long_add_return(pages, &bpf_jit_current) >
533 (bpf_jit_limit >> PAGE_SHIFT)) {
534 if (!capable(CAP_SYS_ADMIN)) {
535 atomic_long_sub(pages, &bpf_jit_current);
536 return -EPERM;
537 }
538 }
539
540 return 0;
541}
542
543static void bpf_jit_uncharge_modmem(u32 pages)
544{
545 atomic_long_sub(pages, &bpf_jit_current);
546}
547
738cbe72
DB
548struct bpf_binary_header *
549bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
550 unsigned int alignment,
551 bpf_jit_fill_hole_t bpf_fill_ill_insns)
552{
553 struct bpf_binary_header *hdr;
409a37eb 554 u32 size, hole, start, pages;
738cbe72
DB
555
556 /* Most of BPF filters are really small, but if some of them
557 * fill a page, allow at least 128 extra bytes to insert a
558 * random section of illegal instructions.
559 */
560 size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
409a37eb
DB
561 pages = size / PAGE_SIZE;
562
563 if (bpf_jit_charge_modmem(pages))
564 return NULL;
738cbe72 565 hdr = module_alloc(size);
409a37eb
DB
566 if (!hdr) {
567 bpf_jit_uncharge_modmem(pages);
738cbe72 568 return NULL;
409a37eb 569 }
738cbe72
DB
570
571 /* Fill space with illegal/arch-dep instructions. */
572 bpf_fill_ill_insns(hdr, size);
573
409a37eb 574 hdr->pages = pages;
738cbe72
DB
575 hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
576 PAGE_SIZE - sizeof(*hdr));
b7552e1b 577 start = (get_random_int() % hole) & ~(alignment - 1);
738cbe72
DB
578
579 /* Leave a random number of instructions before BPF code. */
580 *image_ptr = &hdr->image[start];
581
582 return hdr;
583}
584
585void bpf_jit_binary_free(struct bpf_binary_header *hdr)
586{
409a37eb
DB
587 u32 pages = hdr->pages;
588
be1f221c 589 module_memfree(hdr);
409a37eb 590 bpf_jit_uncharge_modmem(pages);
738cbe72 591}
4f3446bb 592
74451e66
DB
593/* This symbol is only overridden by archs that have different
594 * requirements than the usual eBPF JITs, f.e. when they only
595 * implement cBPF JIT, do not set images read-only, etc.
596 */
597void __weak bpf_jit_free(struct bpf_prog *fp)
598{
599 if (fp->jited) {
600 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
601
602 bpf_jit_binary_unlock_ro(hdr);
603 bpf_jit_binary_free(hdr);
604
605 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
606 }
607
608 bpf_prog_unlock_free(fp);
609}
610
4f3446bb
DB
611static int bpf_jit_blind_insn(const struct bpf_insn *from,
612 const struct bpf_insn *aux,
613 struct bpf_insn *to_buff)
614{
615 struct bpf_insn *to = to_buff;
b7552e1b 616 u32 imm_rnd = get_random_int();
4f3446bb
DB
617 s16 off;
618
619 BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
620 BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
621
92c22ad6
DB
622 /* Constraints on AX register:
623 *
624 * AX register is inaccessible from user space. It is mapped in
625 * all JITs, and used here for constant blinding rewrites. It is
626 * typically "stateless" meaning its contents are only valid within
627 * the executed instruction, but not across several instructions.
628 * There are a few exceptions however which are further detailed
629 * below.
630 *
631 * Constant blinding is only used by JITs, not in the interpreter.
632 * The interpreter uses AX in some occasions as a local temporary
633 * register e.g. in DIV or MOD instructions.
634 *
635 * In restricted circumstances, the verifier can also use the AX
636 * register for rewrites as long as they do not interfere with
637 * the above cases!
638 */
639 if (from->dst_reg == BPF_REG_AX || from->src_reg == BPF_REG_AX)
640 goto out;
641
4f3446bb
DB
642 if (from->imm == 0 &&
643 (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
644 from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
645 *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
646 goto out;
647 }
648
649 switch (from->code) {
650 case BPF_ALU | BPF_ADD | BPF_K:
651 case BPF_ALU | BPF_SUB | BPF_K:
652 case BPF_ALU | BPF_AND | BPF_K:
653 case BPF_ALU | BPF_OR | BPF_K:
654 case BPF_ALU | BPF_XOR | BPF_K:
655 case BPF_ALU | BPF_MUL | BPF_K:
656 case BPF_ALU | BPF_MOV | BPF_K:
657 case BPF_ALU | BPF_DIV | BPF_K:
658 case BPF_ALU | BPF_MOD | BPF_K:
659 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
660 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
661 *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
662 break;
663
664 case BPF_ALU64 | BPF_ADD | BPF_K:
665 case BPF_ALU64 | BPF_SUB | BPF_K:
666 case BPF_ALU64 | BPF_AND | BPF_K:
667 case BPF_ALU64 | BPF_OR | BPF_K:
668 case BPF_ALU64 | BPF_XOR | BPF_K:
669 case BPF_ALU64 | BPF_MUL | BPF_K:
670 case BPF_ALU64 | BPF_MOV | BPF_K:
671 case BPF_ALU64 | BPF_DIV | BPF_K:
672 case BPF_ALU64 | BPF_MOD | BPF_K:
673 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
674 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
675 *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
676 break;
677
678 case BPF_JMP | BPF_JEQ | BPF_K:
679 case BPF_JMP | BPF_JNE | BPF_K:
680 case BPF_JMP | BPF_JGT | BPF_K:
92b31a9a 681 case BPF_JMP | BPF_JLT | BPF_K:
4f3446bb 682 case BPF_JMP | BPF_JGE | BPF_K:
92b31a9a 683 case BPF_JMP | BPF_JLE | BPF_K:
4f3446bb 684 case BPF_JMP | BPF_JSGT | BPF_K:
92b31a9a 685 case BPF_JMP | BPF_JSLT | BPF_K:
4f3446bb 686 case BPF_JMP | BPF_JSGE | BPF_K:
92b31a9a 687 case BPF_JMP | BPF_JSLE | BPF_K:
4f3446bb
DB
688 case BPF_JMP | BPF_JSET | BPF_K:
689 /* Accommodate for extra offset in case of a backjump. */
690 off = from->off;
691 if (off < 0)
692 off -= 2;
693 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
694 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
695 *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
696 break;
697
698 case BPF_LD | BPF_ABS | BPF_W:
699 case BPF_LD | BPF_ABS | BPF_H:
700 case BPF_LD | BPF_ABS | BPF_B:
701 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
702 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
703 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
704 break;
705
706 case BPF_LD | BPF_IND | BPF_W:
707 case BPF_LD | BPF_IND | BPF_H:
708 case BPF_LD | BPF_IND | BPF_B:
709 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
710 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
711 *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
712 *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
713 break;
714
715 case BPF_LD | BPF_IMM | BPF_DW:
716 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
717 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
718 *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
719 *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
720 break;
721 case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
722 *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
723 *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
724 *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
725 break;
726
727 case BPF_ST | BPF_MEM | BPF_DW:
728 case BPF_ST | BPF_MEM | BPF_W:
729 case BPF_ST | BPF_MEM | BPF_H:
730 case BPF_ST | BPF_MEM | BPF_B:
731 *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
732 *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
733 *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
734 break;
735 }
736out:
737 return to - to_buff;
738}
739
740static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
741 gfp_t gfp_extra_flags)
742{
19809c2d 743 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
4f3446bb
DB
744 struct bpf_prog *fp;
745
746 fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
747 if (fp != NULL) {
4f3446bb
DB
748 /* aux->prog still points to the fp_other one, so
749 * when promoting the clone to the real program,
750 * this still needs to be adapted.
751 */
752 memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
753 }
754
755 return fp;
756}
757
758static void bpf_prog_clone_free(struct bpf_prog *fp)
759{
760 /* aux was stolen by the other clone, so we cannot free
761 * it from this path! It will be freed eventually by the
762 * other program on release.
763 *
764 * At this point, we don't need a deferred release since
765 * clone is guaranteed to not be locked.
766 */
767 fp->aux = NULL;
768 __bpf_prog_free(fp);
769}
770
771void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
772{
773 /* We have to repoint aux->prog to self, as we don't
774 * know whether fp here is the clone or the original.
775 */
776 fp->aux->prog = fp;
777 bpf_prog_clone_free(fp_other);
778}
779
780struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
781{
782 struct bpf_insn insn_buff[16], aux[2];
783 struct bpf_prog *clone, *tmp;
784 int insn_delta, insn_cnt;
785 struct bpf_insn *insn;
786 int i, rewritten;
787
788 if (!bpf_jit_blinding_enabled())
789 return prog;
790
791 clone = bpf_prog_clone_create(prog, GFP_USER);
792 if (!clone)
793 return ERR_PTR(-ENOMEM);
794
795 insn_cnt = clone->len;
796 insn = clone->insnsi;
797
798 for (i = 0; i < insn_cnt; i++, insn++) {
799 /* We temporarily need to hold the original ld64 insn
800 * so that we can still access the first part in the
801 * second blinding run.
802 */
803 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
804 insn[1].code == 0)
805 memcpy(aux, insn, sizeof(aux));
806
807 rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
808 if (!rewritten)
809 continue;
810
811 tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
812 if (!tmp) {
813 /* Patching may have repointed aux->prog during
814 * realloc from the original one, so we need to
815 * fix it up here on error.
816 */
817 bpf_jit_prog_release_other(prog, clone);
818 return ERR_PTR(-ENOMEM);
819 }
820
821 clone = tmp;
822 insn_delta = rewritten - 1;
823
824 /* Walk new program and skip insns we just inserted. */
825 insn = clone->insnsi + i + insn_delta;
826 insn_cnt += insn_delta;
827 i += insn_delta;
828 }
829
830 return clone;
831}
b954d834 832#endif /* CONFIG_BPF_JIT */
738cbe72 833
f5bffecd
AS
834/* Base function for offset calculation. Needs to go into .text section,
835 * therefore keeping it non-static as well; will also be used by JITs
836 * anyway later on, so do not let the compiler omit it.
837 */
838noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
839{
840 return 0;
841}
4d9c5c53 842EXPORT_SYMBOL_GPL(__bpf_call_base);
f5bffecd 843
290af866 844#ifndef CONFIG_BPF_JIT_ALWAYS_ON
f5bffecd 845/**
7ae457c1
AS
846 * __bpf_prog_run - run eBPF program on a given context
847 * @ctx: is the data we are operating on
848 * @insn: is the array of eBPF instructions
f5bffecd 849 *
7ae457c1 850 * Decode and execute eBPF instructions.
f5bffecd 851 */
f696b8f4
AS
852static unsigned int ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn,
853 u64 *stack)
f5bffecd 854{
f5bffecd
AS
855 static const void *jumptable[256] = {
856 [0 ... 255] = &&default_label,
857 /* Now overwrite non-defaults ... */
858 /* 32 bit ALU operations */
859 [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
860 [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
861 [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
862 [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
863 [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
864 [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
865 [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X,
866 [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K,
867 [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
868 [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
869 [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
870 [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
871 [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
872 [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
873 [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
874 [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
875 [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
876 [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
877 [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
878 [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
879 [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
880 [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
881 [BPF_ALU | BPF_NEG] = &&ALU_NEG,
882 [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
883 [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
884 /* 64 bit ALU operations */
885 [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
886 [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
887 [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
888 [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
889 [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
890 [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
891 [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
892 [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
893 [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
894 [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
895 [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
896 [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
897 [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
898 [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
899 [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
900 [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
901 [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
902 [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
903 [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
904 [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
905 [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
906 [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
907 [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
908 [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
909 [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
910 /* Call instruction */
911 [BPF_JMP | BPF_CALL] = &&JMP_CALL,
71189fa9 912 [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
f5bffecd
AS
913 /* Jumps */
914 [BPF_JMP | BPF_JA] = &&JMP_JA,
915 [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
916 [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
917 [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
918 [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
919 [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
920 [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
92b31a9a
DB
921 [BPF_JMP | BPF_JLT | BPF_X] = &&JMP_JLT_X,
922 [BPF_JMP | BPF_JLT | BPF_K] = &&JMP_JLT_K,
f5bffecd
AS
923 [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
924 [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
92b31a9a
DB
925 [BPF_JMP | BPF_JLE | BPF_X] = &&JMP_JLE_X,
926 [BPF_JMP | BPF_JLE | BPF_K] = &&JMP_JLE_K,
f5bffecd
AS
927 [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
928 [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
92b31a9a
DB
929 [BPF_JMP | BPF_JSLT | BPF_X] = &&JMP_JSLT_X,
930 [BPF_JMP | BPF_JSLT | BPF_K] = &&JMP_JSLT_K,
f5bffecd
AS
931 [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
932 [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
92b31a9a
DB
933 [BPF_JMP | BPF_JSLE | BPF_X] = &&JMP_JSLE_X,
934 [BPF_JMP | BPF_JSLE | BPF_K] = &&JMP_JSLE_K,
f5bffecd
AS
935 [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
936 [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
937 /* Program return */
938 [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
939 /* Store instructions */
940 [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
941 [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
942 [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
943 [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
944 [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
945 [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
946 [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
947 [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
948 [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
949 [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
950 /* Load instructions */
951 [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
952 [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
953 [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
954 [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
955 [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
956 [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
957 [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
958 [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
959 [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
960 [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
02ab695b 961 [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
f5bffecd 962 };
04fd61ab 963 u32 tail_call_cnt = 0;
f5bffecd
AS
964 void *ptr;
965 int off;
966
967#define CONT ({ insn++; goto select_insn; })
968#define CONT_JMP ({ insn++; goto select_insn; })
969
f5bffecd
AS
970select_insn:
971 goto *jumptable[insn->code];
972
973 /* ALU */
974#define ALU(OPCODE, OP) \
975 ALU64_##OPCODE##_X: \
976 DST = DST OP SRC; \
977 CONT; \
978 ALU_##OPCODE##_X: \
979 DST = (u32) DST OP (u32) SRC; \
980 CONT; \
981 ALU64_##OPCODE##_K: \
982 DST = DST OP IMM; \
983 CONT; \
984 ALU_##OPCODE##_K: \
985 DST = (u32) DST OP (u32) IMM; \
986 CONT;
987
988 ALU(ADD, +)
989 ALU(SUB, -)
990 ALU(AND, &)
991 ALU(OR, |)
992 ALU(LSH, <<)
993 ALU(RSH, >>)
994 ALU(XOR, ^)
995 ALU(MUL, *)
996#undef ALU
997 ALU_NEG:
998 DST = (u32) -DST;
999 CONT;
1000 ALU64_NEG:
1001 DST = -DST;
1002 CONT;
1003 ALU_MOV_X:
1004 DST = (u32) SRC;
1005 CONT;
1006 ALU_MOV_K:
1007 DST = (u32) IMM;
1008 CONT;
1009 ALU64_MOV_X:
1010 DST = SRC;
1011 CONT;
1012 ALU64_MOV_K:
1013 DST = IMM;
1014 CONT;
02ab695b
AS
1015 LD_IMM_DW:
1016 DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
1017 insn++;
1018 CONT;
f5bffecd
AS
1019 ALU64_ARSH_X:
1020 (*(s64 *) &DST) >>= SRC;
1021 CONT;
1022 ALU64_ARSH_K:
1023 (*(s64 *) &DST) >>= IMM;
1024 CONT;
1025 ALU64_MOD_X:
1026 if (unlikely(SRC == 0))
1027 return 0;
1ead6876
DB
1028 div64_u64_rem(DST, SRC, &AX);
1029 DST = AX;
f5bffecd
AS
1030 CONT;
1031 ALU_MOD_X:
c366287e 1032 if (unlikely((u32)SRC == 0))
f5bffecd 1033 return 0;
1ead6876
DB
1034 AX = (u32) DST;
1035 DST = do_div(AX, (u32) SRC);
f5bffecd
AS
1036 CONT;
1037 ALU64_MOD_K:
1ead6876
DB
1038 div64_u64_rem(DST, IMM, &AX);
1039 DST = AX;
f5bffecd
AS
1040 CONT;
1041 ALU_MOD_K:
1ead6876
DB
1042 AX = (u32) DST;
1043 DST = do_div(AX, (u32) IMM);
f5bffecd
AS
1044 CONT;
1045 ALU64_DIV_X:
1046 if (unlikely(SRC == 0))
1047 return 0;
876a7ae6 1048 DST = div64_u64(DST, SRC);
f5bffecd
AS
1049 CONT;
1050 ALU_DIV_X:
c366287e 1051 if (unlikely((u32)SRC == 0))
f5bffecd 1052 return 0;
1ead6876
DB
1053 AX = (u32) DST;
1054 do_div(AX, (u32) SRC);
1055 DST = (u32) AX;
f5bffecd
AS
1056 CONT;
1057 ALU64_DIV_K:
876a7ae6 1058 DST = div64_u64(DST, IMM);
f5bffecd
AS
1059 CONT;
1060 ALU_DIV_K:
1ead6876
DB
1061 AX = (u32) DST;
1062 do_div(AX, (u32) IMM);
1063 DST = (u32) AX;
f5bffecd
AS
1064 CONT;
1065 ALU_END_TO_BE:
1066 switch (IMM) {
1067 case 16:
1068 DST = (__force u16) cpu_to_be16(DST);
1069 break;
1070 case 32:
1071 DST = (__force u32) cpu_to_be32(DST);
1072 break;
1073 case 64:
1074 DST = (__force u64) cpu_to_be64(DST);
1075 break;
1076 }
1077 CONT;
1078 ALU_END_TO_LE:
1079 switch (IMM) {
1080 case 16:
1081 DST = (__force u16) cpu_to_le16(DST);
1082 break;
1083 case 32:
1084 DST = (__force u32) cpu_to_le32(DST);
1085 break;
1086 case 64:
1087 DST = (__force u64) cpu_to_le64(DST);
1088 break;
1089 }
1090 CONT;
1091
1092 /* CALL */
1093 JMP_CALL:
1094 /* Function call scratches BPF_R1-BPF_R5 registers,
1095 * preserves BPF_R6-BPF_R9, and stores return value
1096 * into BPF_R0.
1097 */
1098 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
1099 BPF_R4, BPF_R5);
1100 CONT;
1101
04fd61ab
AS
1102 JMP_TAIL_CALL: {
1103 struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
1104 struct bpf_array *array = container_of(map, struct bpf_array, map);
1105 struct bpf_prog *prog;
90caccdd 1106 u32 index = BPF_R3;
04fd61ab
AS
1107
1108 if (unlikely(index >= array->map.max_entries))
1109 goto out;
04fd61ab
AS
1110 if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
1111 goto out;
1112
1113 tail_call_cnt++;
1114
2a36f0b9 1115 prog = READ_ONCE(array->ptrs[index]);
1ca1cc98 1116 if (!prog)
04fd61ab
AS
1117 goto out;
1118
c4675f93
DB
1119 /* ARG1 at this point is guaranteed to point to CTX from
1120 * the verifier side due to the fact that the tail call is
1121 * handeled like a helper, that is, bpf_tail_call_proto,
1122 * where arg1_type is ARG_PTR_TO_CTX.
1123 */
04fd61ab
AS
1124 insn = prog->insnsi;
1125 goto select_insn;
1126out:
1127 CONT;
1128 }
f5bffecd
AS
1129 /* JMP */
1130 JMP_JA:
1131 insn += insn->off;
1132 CONT;
1133 JMP_JEQ_X:
1134 if (DST == SRC) {
1135 insn += insn->off;
1136 CONT_JMP;
1137 }
1138 CONT;
1139 JMP_JEQ_K:
1140 if (DST == IMM) {
1141 insn += insn->off;
1142 CONT_JMP;
1143 }
1144 CONT;
1145 JMP_JNE_X:
1146 if (DST != SRC) {
1147 insn += insn->off;
1148 CONT_JMP;
1149 }
1150 CONT;
1151 JMP_JNE_K:
1152 if (DST != IMM) {
1153 insn += insn->off;
1154 CONT_JMP;
1155 }
1156 CONT;
1157 JMP_JGT_X:
1158 if (DST > SRC) {
1159 insn += insn->off;
1160 CONT_JMP;
1161 }
1162 CONT;
1163 JMP_JGT_K:
1164 if (DST > IMM) {
1165 insn += insn->off;
1166 CONT_JMP;
1167 }
1168 CONT;
92b31a9a
DB
1169 JMP_JLT_X:
1170 if (DST < SRC) {
1171 insn += insn->off;
1172 CONT_JMP;
1173 }
1174 CONT;
1175 JMP_JLT_K:
1176 if (DST < IMM) {
1177 insn += insn->off;
1178 CONT_JMP;
1179 }
1180 CONT;
f5bffecd
AS
1181 JMP_JGE_X:
1182 if (DST >= SRC) {
1183 insn += insn->off;
1184 CONT_JMP;
1185 }
1186 CONT;
1187 JMP_JGE_K:
1188 if (DST >= IMM) {
1189 insn += insn->off;
1190 CONT_JMP;
1191 }
1192 CONT;
92b31a9a
DB
1193 JMP_JLE_X:
1194 if (DST <= SRC) {
1195 insn += insn->off;
1196 CONT_JMP;
1197 }
1198 CONT;
1199 JMP_JLE_K:
1200 if (DST <= IMM) {
1201 insn += insn->off;
1202 CONT_JMP;
1203 }
1204 CONT;
f5bffecd
AS
1205 JMP_JSGT_X:
1206 if (((s64) DST) > ((s64) SRC)) {
1207 insn += insn->off;
1208 CONT_JMP;
1209 }
1210 CONT;
1211 JMP_JSGT_K:
1212 if (((s64) DST) > ((s64) IMM)) {
1213 insn += insn->off;
1214 CONT_JMP;
1215 }
1216 CONT;
92b31a9a
DB
1217 JMP_JSLT_X:
1218 if (((s64) DST) < ((s64) SRC)) {
1219 insn += insn->off;
1220 CONT_JMP;
1221 }
1222 CONT;
1223 JMP_JSLT_K:
1224 if (((s64) DST) < ((s64) IMM)) {
1225 insn += insn->off;
1226 CONT_JMP;
1227 }
1228 CONT;
f5bffecd
AS
1229 JMP_JSGE_X:
1230 if (((s64) DST) >= ((s64) SRC)) {
1231 insn += insn->off;
1232 CONT_JMP;
1233 }
1234 CONT;
1235 JMP_JSGE_K:
1236 if (((s64) DST) >= ((s64) IMM)) {
1237 insn += insn->off;
1238 CONT_JMP;
1239 }
1240 CONT;
92b31a9a
DB
1241 JMP_JSLE_X:
1242 if (((s64) DST) <= ((s64) SRC)) {
1243 insn += insn->off;
1244 CONT_JMP;
1245 }
1246 CONT;
1247 JMP_JSLE_K:
1248 if (((s64) DST) <= ((s64) IMM)) {
1249 insn += insn->off;
1250 CONT_JMP;
1251 }
1252 CONT;
f5bffecd
AS
1253 JMP_JSET_X:
1254 if (DST & SRC) {
1255 insn += insn->off;
1256 CONT_JMP;
1257 }
1258 CONT;
1259 JMP_JSET_K:
1260 if (DST & IMM) {
1261 insn += insn->off;
1262 CONT_JMP;
1263 }
1264 CONT;
1265 JMP_EXIT:
1266 return BPF_R0;
1267
1268 /* STX and ST and LDX*/
1269#define LDST(SIZEOP, SIZE) \
1270 STX_MEM_##SIZEOP: \
1271 *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
1272 CONT; \
1273 ST_MEM_##SIZEOP: \
1274 *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
1275 CONT; \
1276 LDX_MEM_##SIZEOP: \
1277 DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
1278 CONT;
1279
1280 LDST(B, u8)
1281 LDST(H, u16)
1282 LDST(W, u32)
1283 LDST(DW, u64)
1284#undef LDST
1285 STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
1286 atomic_add((u32) SRC, (atomic_t *)(unsigned long)
1287 (DST + insn->off));
1288 CONT;
1289 STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
1290 atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
1291 (DST + insn->off));
1292 CONT;
1293 LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
1294 off = IMM;
1295load_word:
96a94cc5
JB
1296 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are only
1297 * appearing in the programs where ctx == skb
1298 * (see may_access_skb() in the verifier). All programs
1299 * keep 'ctx' in regs[BPF_REG_CTX] == BPF_R6,
1300 * bpf_convert_filter() saves it in BPF_R6, internal BPF
1301 * verifier will check that BPF_R6 == ctx.
f5bffecd
AS
1302 *
1303 * BPF_ABS and BPF_IND are wrappers of function calls,
1304 * so they scratch BPF_R1-BPF_R5 registers, preserve
1305 * BPF_R6-BPF_R9, and store return value into BPF_R0.
1306 *
1307 * Implicit input:
1308 * ctx == skb == BPF_R6 == CTX
1309 *
1310 * Explicit input:
1311 * SRC == any register
1312 * IMM == 32-bit immediate
1313 *
1314 * Output:
1315 * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
1316 */
1317
1ead6876 1318 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &AX);
f5bffecd
AS
1319 if (likely(ptr != NULL)) {
1320 BPF_R0 = get_unaligned_be32(ptr);
1321 CONT;
1322 }
1323
1324 return 0;
1325 LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
1326 off = IMM;
1327load_half:
1ead6876 1328 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &AX);
f5bffecd
AS
1329 if (likely(ptr != NULL)) {
1330 BPF_R0 = get_unaligned_be16(ptr);
1331 CONT;
1332 }
1333
1334 return 0;
1335 LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
1336 off = IMM;
1337load_byte:
1ead6876 1338 ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &AX);
f5bffecd
AS
1339 if (likely(ptr != NULL)) {
1340 BPF_R0 = *(u8 *)ptr;
1341 CONT;
1342 }
1343
1344 return 0;
1345 LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
1346 off = IMM + SRC;
1347 goto load_word;
1348 LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
1349 off = IMM + SRC;
1350 goto load_half;
1351 LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
1352 off = IMM + SRC;
1353 goto load_byte;
1354
1355 default_label:
1356 /* If we ever reach this, we have a bug somewhere. */
1357 WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
1358 return 0;
1359}
f696b8f4
AS
1360STACK_FRAME_NON_STANDARD(___bpf_prog_run); /* jump table */
1361
b870aa90
AS
1362#define PROG_NAME(stack_size) __bpf_prog_run##stack_size
1363#define DEFINE_BPF_PROG_RUN(stack_size) \
1364static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
1365{ \
1366 u64 stack[stack_size / sizeof(u64)]; \
1ead6876 1367 u64 regs[MAX_BPF_EXT_REG]; \
b870aa90
AS
1368\
1369 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
1370 ARG1 = (u64) (unsigned long) ctx; \
1371 return ___bpf_prog_run(regs, insn, stack); \
f696b8f4 1372}
f5bffecd 1373
b870aa90
AS
1374#define EVAL1(FN, X) FN(X)
1375#define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
1376#define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
1377#define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
1378#define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
1379#define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
1380
1381EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
1382EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
1383EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
1384
1385#define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
1386
1387static unsigned int (*interpreters[])(const void *ctx,
1388 const struct bpf_insn *insn) = {
1389EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
1390EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
1391EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
1392};
1393
290af866 1394#else
69e97d45
DB
1395static unsigned int __bpf_prog_ret0_warn(const void *ctx,
1396 const struct bpf_insn *insn)
290af866 1397{
69e97d45
DB
1398 /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
1399 * is not working properly, so warn about it!
1400 */
1401 WARN_ON_ONCE(1);
290af866
AS
1402 return 0;
1403}
1404#endif
1405
3324b584
DB
1406bool bpf_prog_array_compatible(struct bpf_array *array,
1407 const struct bpf_prog *fp)
04fd61ab 1408{
3324b584
DB
1409 if (!array->owner_prog_type) {
1410 /* There's no owner yet where we could check for
1411 * compatibility.
1412 */
04fd61ab
AS
1413 array->owner_prog_type = fp->type;
1414 array->owner_jited = fp->jited;
3324b584
DB
1415
1416 return true;
04fd61ab 1417 }
3324b584
DB
1418
1419 return array->owner_prog_type == fp->type &&
1420 array->owner_jited == fp->jited;
04fd61ab
AS
1421}
1422
3324b584 1423static int bpf_check_tail_call(const struct bpf_prog *fp)
04fd61ab
AS
1424{
1425 struct bpf_prog_aux *aux = fp->aux;
1426 int i;
1427
1428 for (i = 0; i < aux->used_map_cnt; i++) {
3324b584 1429 struct bpf_map *map = aux->used_maps[i];
04fd61ab 1430 struct bpf_array *array;
04fd61ab 1431
04fd61ab
AS
1432 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
1433 continue;
3324b584 1434
04fd61ab
AS
1435 array = container_of(map, struct bpf_array, map);
1436 if (!bpf_prog_array_compatible(array, fp))
1437 return -EINVAL;
1438 }
1439
1440 return 0;
1441}
1442
f5bffecd 1443/**
3324b584 1444 * bpf_prog_select_runtime - select exec runtime for BPF program
7ae457c1 1445 * @fp: bpf_prog populated with internal BPF program
d1c55ab5 1446 * @err: pointer to error variable
f5bffecd 1447 *
3324b584
DB
1448 * Try to JIT eBPF program, if JIT is not available, use interpreter.
1449 * The BPF program will be executed via BPF_PROG_RUN() macro.
f5bffecd 1450 */
d1c55ab5 1451struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
f5bffecd 1452{
290af866 1453#ifndef CONFIG_BPF_JIT_ALWAYS_ON
8007e40a
MKL
1454 u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
1455
1456 fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
290af866 1457#else
69e97d45 1458 fp->bpf_func = __bpf_prog_ret0_warn;
290af866 1459#endif
f5bffecd 1460
d1c55ab5
DB
1461 /* eBPF JITs can rewrite the program in case constant
1462 * blinding is active. However, in case of error during
1463 * blinding, bpf_int_jit_compile() must always return a
1464 * valid program, which in this case would simply not
1465 * be JITed, but falls back to the interpreter.
1466 */
ab3f0063
JK
1467 if (!bpf_prog_is_dev_bound(fp->aux)) {
1468 fp = bpf_int_jit_compile(fp);
290af866
AS
1469#ifdef CONFIG_BPF_JIT_ALWAYS_ON
1470 if (!fp->jited) {
1471 *err = -ENOTSUPP;
1472 return fp;
1473 }
1474#endif
ab3f0063
JK
1475 } else {
1476 *err = bpf_prog_offload_compile(fp);
1477 if (*err)
1478 return fp;
1479 }
60a3b225 1480 bpf_prog_lock_ro(fp);
04fd61ab 1481
3324b584
DB
1482 /* The tail call compatibility check can only be done at
1483 * this late stage as we need to determine, if we deal
1484 * with JITed or non JITed program concatenations and not
1485 * all eBPF JITs might immediately support all features.
1486 */
d1c55ab5
DB
1487 *err = bpf_check_tail_call(fp);
1488
1489 return fp;
f5bffecd 1490}
7ae457c1 1491EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
f5bffecd 1492
e87c6bc3
YS
1493static unsigned int __bpf_prog_ret1(const void *ctx,
1494 const struct bpf_insn *insn)
1495{
1496 return 1;
1497}
1498
1499static struct bpf_prog_dummy {
1500 struct bpf_prog prog;
1501} dummy_bpf_prog = {
1502 .prog = {
1503 .bpf_func = __bpf_prog_ret1,
1504 },
1505};
1506
324bda9e
AS
1507/* to avoid allocating empty bpf_prog_array for cgroups that
1508 * don't have bpf program attached use one global 'empty_prog_array'
1509 * It will not be modified the caller of bpf_prog_array_alloc()
1510 * (since caller requested prog_cnt == 0)
1511 * that pointer should be 'freed' by bpf_prog_array_free()
1512 */
1513static struct {
1514 struct bpf_prog_array hdr;
1515 struct bpf_prog *null_prog;
1516} empty_prog_array = {
1517 .null_prog = NULL,
1518};
1519
1520struct bpf_prog_array __rcu *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
1521{
1522 if (prog_cnt)
1523 return kzalloc(sizeof(struct bpf_prog_array) +
1524 sizeof(struct bpf_prog *) * (prog_cnt + 1),
1525 flags);
1526
1527 return &empty_prog_array.hdr;
1528}
1529
1530void bpf_prog_array_free(struct bpf_prog_array __rcu *progs)
1531{
1532 if (!progs ||
1533 progs == (struct bpf_prog_array __rcu *)&empty_prog_array.hdr)
1534 return;
1535 kfree_rcu(progs, rcu);
1536}
1537
468e2f64
AS
1538int bpf_prog_array_length(struct bpf_prog_array __rcu *progs)
1539{
1540 struct bpf_prog **prog;
1541 u32 cnt = 0;
1542
1543 rcu_read_lock();
1544 prog = rcu_dereference(progs)->progs;
1545 for (; *prog; prog++)
c8c088ba
YS
1546 if (*prog != &dummy_bpf_prog.prog)
1547 cnt++;
468e2f64
AS
1548 rcu_read_unlock();
1549 return cnt;
1550}
1551
1552int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs,
1553 __u32 __user *prog_ids, u32 cnt)
1554{
1555 struct bpf_prog **prog;
1556 u32 i = 0, id;
1557
1558 rcu_read_lock();
1559 prog = rcu_dereference(progs)->progs;
1560 for (; *prog; prog++) {
1561 id = (*prog)->aux->id;
1562 if (copy_to_user(prog_ids + i, &id, sizeof(id))) {
1563 rcu_read_unlock();
1564 return -EFAULT;
1565 }
1566 if (++i == cnt) {
1567 prog++;
1568 break;
1569 }
1570 }
1571 rcu_read_unlock();
1572 if (*prog)
1573 return -ENOSPC;
1574 return 0;
1575}
1576
e87c6bc3
YS
1577void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs,
1578 struct bpf_prog *old_prog)
1579{
1580 struct bpf_prog **prog = progs->progs;
1581
1582 for (; *prog; prog++)
1583 if (*prog == old_prog) {
1584 WRITE_ONCE(*prog, &dummy_bpf_prog.prog);
1585 break;
1586 }
1587}
1588
1589int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
1590 struct bpf_prog *exclude_prog,
1591 struct bpf_prog *include_prog,
1592 struct bpf_prog_array **new_array)
1593{
1594 int new_prog_cnt, carry_prog_cnt = 0;
1595 struct bpf_prog **existing_prog;
1596 struct bpf_prog_array *array;
1597 int new_prog_idx = 0;
1598
1599 /* Figure out how many existing progs we need to carry over to
1600 * the new array.
1601 */
1602 if (old_array) {
1603 existing_prog = old_array->progs;
1604 for (; *existing_prog; existing_prog++) {
1605 if (*existing_prog != exclude_prog &&
1606 *existing_prog != &dummy_bpf_prog.prog)
1607 carry_prog_cnt++;
1608 if (*existing_prog == include_prog)
1609 return -EEXIST;
1610 }
1611 }
1612
1613 /* How many progs (not NULL) will be in the new array? */
1614 new_prog_cnt = carry_prog_cnt;
1615 if (include_prog)
1616 new_prog_cnt += 1;
1617
1618 /* Do we have any prog (not NULL) in the new array? */
1619 if (!new_prog_cnt) {
1620 *new_array = NULL;
1621 return 0;
1622 }
1623
1624 /* +1 as the end of prog_array is marked with NULL */
1625 array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
1626 if (!array)
1627 return -ENOMEM;
1628
1629 /* Fill in the new prog array */
1630 if (carry_prog_cnt) {
1631 existing_prog = old_array->progs;
1632 for (; *existing_prog; existing_prog++)
1633 if (*existing_prog != exclude_prog &&
1634 *existing_prog != &dummy_bpf_prog.prog)
1635 array->progs[new_prog_idx++] = *existing_prog;
1636 }
1637 if (include_prog)
1638 array->progs[new_prog_idx++] = include_prog;
1639 array->progs[new_prog_idx] = NULL;
1640 *new_array = array;
1641 return 0;
1642}
1643
60a3b225
DB
1644static void bpf_prog_free_deferred(struct work_struct *work)
1645{
09756af4 1646 struct bpf_prog_aux *aux;
60a3b225 1647
09756af4 1648 aux = container_of(work, struct bpf_prog_aux, work);
ab3f0063
JK
1649 if (bpf_prog_is_dev_bound(aux))
1650 bpf_prog_offload_destroy(aux->prog);
09756af4 1651 bpf_jit_free(aux->prog);
60a3b225
DB
1652}
1653
1654/* Free internal BPF program */
7ae457c1 1655void bpf_prog_free(struct bpf_prog *fp)
f5bffecd 1656{
09756af4 1657 struct bpf_prog_aux *aux = fp->aux;
60a3b225 1658
09756af4 1659 INIT_WORK(&aux->work, bpf_prog_free_deferred);
09756af4 1660 schedule_work(&aux->work);
f5bffecd 1661}
7ae457c1 1662EXPORT_SYMBOL_GPL(bpf_prog_free);
f89b7755 1663
3ad00405
DB
1664/* RNG for unpriviledged user space with separated state from prandom_u32(). */
1665static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
1666
1667void bpf_user_rnd_init_once(void)
1668{
1669 prandom_init_once(&bpf_user_rnd_state);
1670}
1671
f3694e00 1672BPF_CALL_0(bpf_user_rnd_u32)
3ad00405
DB
1673{
1674 /* Should someone ever have the rather unwise idea to use some
1675 * of the registers passed into this function, then note that
1676 * this function is called from native eBPF and classic-to-eBPF
1677 * transformations. Register assignments from both sides are
1678 * different, f.e. classic always sets fn(ctx, A, X) here.
1679 */
1680 struct rnd_state *state;
1681 u32 res;
1682
1683 state = &get_cpu_var(bpf_user_rnd_state);
1684 res = prandom_u32_state(state);
b761fe22 1685 put_cpu_var(bpf_user_rnd_state);
3ad00405
DB
1686
1687 return res;
1688}
1689
3ba67dab
DB
1690/* Weak definitions of helper functions in case we don't have bpf syscall. */
1691const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
1692const struct bpf_func_proto bpf_map_update_elem_proto __weak;
1693const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
1694
03e69b50 1695const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
c04167ce 1696const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
2d0e30c3 1697const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
17ca8cbf 1698const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
bd570ff9 1699
ffeedafb
AS
1700const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
1701const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
1702const struct bpf_func_proto bpf_get_current_comm_proto __weak;
6bdc9c4c 1703const struct bpf_func_proto bpf_sock_map_update_proto __weak;
bd570ff9 1704
0756ea3e
AS
1705const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
1706{
1707 return NULL;
1708}
03e69b50 1709
555c8a86
DB
1710u64 __weak
1711bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1712 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
bd570ff9 1713{
555c8a86 1714 return -ENOTSUPP;
bd570ff9
DB
1715}
1716
3324b584
DB
1717/* Always built-in helper functions. */
1718const struct bpf_func_proto bpf_tail_call_proto = {
1719 .func = NULL,
1720 .gpl_only = false,
1721 .ret_type = RET_VOID,
1722 .arg1_type = ARG_PTR_TO_CTX,
1723 .arg2_type = ARG_CONST_MAP_PTR,
1724 .arg3_type = ARG_ANYTHING,
1725};
1726
9383191d
DB
1727/* Stub for JITs that only support cBPF. eBPF programs are interpreted.
1728 * It is encouraged to implement bpf_int_jit_compile() instead, so that
1729 * eBPF and implicitly also cBPF can get JITed!
1730 */
d1c55ab5 1731struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
3324b584 1732{
d1c55ab5 1733 return prog;
3324b584
DB
1734}
1735
9383191d
DB
1736/* Stub for JITs that support eBPF. All cBPF code gets transformed into
1737 * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
1738 */
1739void __weak bpf_jit_compile(struct bpf_prog *prog)
1740{
1741}
1742
17bedab2 1743bool __weak bpf_helper_changes_pkt_data(void *func)
969bf05e
AS
1744{
1745 return false;
1746}
1747
f89b7755
AS
1748/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
1749 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
1750 */
1751int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
1752 int len)
1753{
1754 return -EFAULT;
1755}
a67edbf4
DB
1756
1757/* All definitions of tracepoints related to BPF. */
1758#define CREATE_TRACE_POINTS
1759#include <linux/bpf_trace.h>
1760
1761EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
1762
9185a610
SRV
1763/* These are only used within the BPF_SYSCALL code */
1764#ifdef CONFIG_BPF_SYSCALL
a67edbf4
DB
1765EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_get_type);
1766EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_put_rcu);
9185a610 1767#endif