/* * Testsuite for BPF interpreter and BPF JIT compiler * * Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/init.h>#include <linux/module.h>#include <linux/filter.h>#include <linux/bpf.h>#include <linux/skbuff.h>#include <linux/netdevice.h>#include <linux/if_vlan.h>#include <linux/random.h>#include <linux/highmem.h> /* General test specific settings */#define MAX_SUBTESTS 3#define MAX_TESTRUNS 10000#define MAX_DATA 128#define MAX_INSNS 512#define MAX_K 0xffffFFFF /* Few constants used to init test 'skb' */#define SKB_TYPE 3#define SKB_MARK 0x1234aaaa#define SKB_HASH 0x1234aaab#define SKB_QUEUE_MAP 123#define SKB_VLAN_TCI 0xffff#define SKB_DEV_IFINDEX 577#define SKB_DEV_TYPE 588 /* Redefine REGs to make tests less verbose */#define R0 BPF_REG_0#define R1 BPF_REG_1#define R2 BPF_REG_2#define R3 BPF_REG_3#define R4 BPF_REG_4#define R5 BPF_REG_5#define R6 BPF_REG_6#define R7 BPF_REG_7#define R8 BPF_REG_8#define R9 BPF_REG_9#define R10 BPF_REG_10 /* Flags that can be passed to test cases */#define FLAG_NO_DATA BIT(0)#define FLAG_EXPECTED_FAIL BIT(1)#define FLAG_SKB_FRAG BIT(2) enum { CLASSIC = BIT(6), /* Old BPF instructions only. */ INTERNAL = BIT(7), /* Extended instruction set. */}; #define TEST_TYPE_MASK (CLASSIC | INTERNAL) struct bpf_test { const char *descr; union { struct sock_filter insns[MAX_INSNS]; struct bpf_insn insns_int[MAX_INSNS]; struct { void *insns; unsigned int len; } ptr; } u; __u8 aux; __u8 data[MAX_DATA]; struct { int data_size; __u32 result; } test[MAX_SUBTESTS]; int (*fill_helper)(struct bpf_test *self); __u8 frag_data[MAX_DATA]; int stack_depth; /* for eBPF only, since tests don't call verifier */}; /* Large test cases need separate allocation and fill handler. */ static int bpf_fill_maxinsns1(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; __u32 k = ~0; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len; i++, k--) insn[i] = __BPF_STMT(BPF_RET | BPF_K, k); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int bpf_fill_maxinsns2(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len; i++) insn[i] = __BPF_STMT(BPF_RET | BPF_K, 0xfefefefe); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int bpf_fill_maxinsns3(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; struct rnd_state rnd; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; prandom_seed_state(&rnd, 3141592653589793238ULL); for (i = 0; i < len - 1; i++) { __u32 k = prandom_u32_state(&rnd); insn[i] = __BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, k); } insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int bpf_fill_maxinsns4(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS + 1; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len; i++) insn[i] = __BPF_STMT(BPF_RET | BPF_K, 0xfefefefe); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int bpf_fill_maxinsns5(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[0] = __BPF_JUMP(BPF_JMP | BPF_JA, len - 2, 0, 0); for (i = 1; i < len - 1; i++) insn[i] = __BPF_STMT(BPF_RET | BPF_K, 0xfefefefe); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_K, 0xabababab); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int bpf_fill_maxinsns6(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len - 1; i++) insn[i] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS, SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int bpf_fill_maxinsns7(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len - 4; i++) insn[i] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS, SKF_AD_OFF + SKF_AD_CPU); insn[len - 4] = __BPF_STMT(BPF_MISC | BPF_TAX, 0); insn[len - 3] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS, SKF_AD_OFF + SKF_AD_CPU); insn[len - 2] = __BPF_STMT(BPF_ALU | BPF_SUB | BPF_X, 0); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int bpf_fill_maxinsns8(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i, jmp_off = len - 3; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[0] = __BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff); for (i = 1; i < len - 1; i++) insn[i] = __BPF_JUMP(BPF_JMP | BPF_JGT, 0xffffffff, jmp_off--, 0); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int bpf_fill_maxinsns9(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS; struct bpf_insn *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[0] = BPF_JMP_IMM(BPF_JA, 0, 0, len - 2); insn[1] = BPF_ALU32_IMM(BPF_MOV, R0, 0xcbababab); insn[2] = BPF_EXIT_INSN(); for (i = 3; i < len - 2; i++) insn[i] = BPF_ALU32_IMM(BPF_MOV, R0, 0xfefefefe); insn[len - 2] = BPF_EXIT_INSN(); insn[len - 1] = BPF_JMP_IMM(BPF_JA, 0, 0, -(len - 1)); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int bpf_fill_maxinsns10(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS, hlen = len - 2; struct bpf_insn *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < hlen / 2; i++) insn[i] = BPF_JMP_IMM(BPF_JA, 0, 0, hlen - 2 - 2 * i); for (i = hlen - 1; i > hlen / 2; i--) insn[i] = BPF_JMP_IMM(BPF_JA, 0, 0, hlen - 1 - 2 * i); insn[hlen / 2] = BPF_JMP_IMM(BPF_JA, 0, 0, hlen / 2 - 1); insn[hlen] = BPF_ALU32_IMM(BPF_MOV, R0, 0xabababac); insn[hlen + 1] = BPF_EXIT_INSN(); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int __bpf_fill_ja(struct bpf_test *self, unsigned int len, unsigned int plen){ struct sock_filter *insn; unsigned int rlen; int i, j; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; rlen = (len % plen) - 1; for (i = 0; i + plen < len; i += plen) for (j = 0; j < plen; j++) insn[i + j] = __BPF_JUMP(BPF_JMP | BPF_JA, plen - 1 - j, 0, 0); for (j = 0; j < rlen; j++) insn[i + j] = __BPF_JUMP(BPF_JMP | BPF_JA, rlen - 1 - j, 0, 0); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_K, 0xababcbac); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int bpf_fill_maxinsns11(struct bpf_test *self){ /* Hits 70 passes on x86_64, so cannot get JITed there. */ return __bpf_fill_ja(self, BPF_MAXINSNS, 68);} static int bpf_fill_ja(struct bpf_test *self){ /* Hits exactly 11 passes on x86_64 JIT. */ return __bpf_fill_ja(self, 12, 9);} static int bpf_fill_ld_abs_get_processor_id(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len - 1; i += 2) { insn[i] = __BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 0); insn[i + 1] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS, SKF_AD_OFF + SKF_AD_CPU); } insn[len - 1] = __BPF_STMT(BPF_RET | BPF_K, 0xbee); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} #define PUSH_CNT 68/* test: {skb->data[0], vlan_push} x 68 + {skb->data[0], vlan_pop} x 68 */static int bpf_fill_ld_abs_vlan_push_pop(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS; struct bpf_insn *insn; int i = 0, j, k = 0; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[i++] = BPF_MOV64_REG(R6, R1);loop: for (j = 0; j < PUSH_CNT; j++) { insn[i++] = BPF_LD_ABS(BPF_B, 0); insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0x34, len - i - 2); i++; insn[i++] = BPF_MOV64_REG(R1, R6); insn[i++] = BPF_MOV64_IMM(R2, 1); insn[i++] = BPF_MOV64_IMM(R3, 2); insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, bpf_skb_vlan_push_proto.func - __bpf_call_base); insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0, len - i - 2); i++; } for (j = 0; j < PUSH_CNT; j++) { insn[i++] = BPF_LD_ABS(BPF_B, 0); insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0x34, len - i - 2); i++; insn[i++] = BPF_MOV64_REG(R1, R6); insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, bpf_skb_vlan_pop_proto.func - __bpf_call_base); insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0, len - i - 2); i++; } if (++k < 5) goto loop; for (; i < len - 1; i++) insn[i] = BPF_ALU32_IMM(BPF_MOV, R0, 0xbef); insn[len - 1] = BPF_EXIT_INSN(); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int bpf_fill_jump_around_ld_abs(struct bpf_test *self){ unsigned int len = BPF_MAXINSNS; struct bpf_insn *insn; int i = 0; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[i++] = BPF_MOV64_REG(R6, R1); insn[i++] = BPF_LD_ABS(BPF_B, 0); insn[i] = BPF_JMP_IMM(BPF_JEQ, R0, 10, len - i - 2); i++; while (i < len - 1) insn[i++] = BPF_LD_ABS(BPF_B, 1); insn[i] = BPF_EXIT_INSN(); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0;} static int __bpf_fill_stxdw(struct bpf_test *self, int size){ unsigned int len = BPF_MAXINSNS; struct bpf_insn *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[0] = BPF_ALU32_IMM(BPF_MOV, R0, 1); insn[1] = BPF_ST_MEM(size, R10, -40, 42); for (i = 2; i < len - 2; i++) insn[i] = BPF_STX_XADD(size, R10, R0, -40); insn[len - 2] = BPF_LDX_MEM(size, R0, R10, -40); insn[len - 1] = BPF_EXIT_INSN(); self->u.ptr.insns = insn; self->u.ptr.len = len; self->stack_depth = 40; return 0;} static int bpf_fill_stxw(struct bpf_test *self){ return __bpf_fill_stxdw(self, BPF_W);} static int bpf_fill_stxdw(struct bpf_test *self){ return __bpf_fill_stxdw(self, BPF_DW);} static struct bpf_test tests[] = { { "TAX",Lines 1–500 of 6106. Load more
/* * Testsuite for BPF interpreter and BPF JIT compiler * * Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/init.h> #include <linux/module.h> #include <linux/filter.h> #include <linux/bpf.h> #include <linux/skbuff.h> #include <linux/netdevice.h> #include <linux/if_vlan.h> #include <linux/random.h> #include <linux/highmem.h> /* General test specific settings */ #define MAX_SUBTESTS 3 #define MAX_TESTRUNS 10000 #define MAX_DATA 128 #define MAX_INSNS 512 #define MAX_K 0xffffFFFF /* Few constants used to init test 'skb' */ #define SKB_TYPE 3 #define SKB_MARK 0x1234aaaa #define SKB_HASH 0x1234aaab #define SKB_QUEUE_MAP 123 #define SKB_VLAN_TCI 0xffff #define SKB_DEV_IFINDEX 577 #define SKB_DEV_TYPE 588 /* Redefine REGs to make tests less verbose */ #define R0 BPF_REG_0 #define R1 BPF_REG_1 #define R2 BPF_REG_2 #define R3 BPF_REG_3 #define R4 BPF_REG_4 #define R5 BPF_REG_5 #define R6 BPF_REG_6 #define R7 BPF_REG_7 #define R8 BPF_REG_8 #define R9 BPF_REG_9 #define R10 BPF_REG_10 /* Flags that can be passed to test cases */ #define FLAG_NO_DATA BIT(0) #define FLAG_EXPECTED_FAIL BIT(1) #define FLAG_SKB_FRAG BIT(2) enum { CLASSIC = BIT(6), /* Old BPF instructions only. */ INTERNAL = BIT(7), /* Extended instruction set. */ }; #define TEST_TYPE_MASK (CLASSIC | INTERNAL) struct bpf_test { const char *descr; union { struct sock_filter insns[MAX_INSNS]; struct bpf_insn insns_int[MAX_INSNS]; struct { void *insns; unsigned int len; } ptr; } u; __u8 aux; __u8 data[MAX_DATA]; struct { int data_size; __u32 result; } test[MAX_SUBTESTS]; int (*fill_helper)(struct bpf_test *self); __u8 frag_data[MAX_DATA]; int stack_depth; /* for eBPF only, since tests don't call verifier */ }; /* Large test cases need separate allocation and fill handler. */ static int bpf_fill_maxinsns1(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; __u32 k = ~0; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len; i++, k--) insn[i] = __BPF_STMT(BPF_RET | BPF_K, k); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int bpf_fill_maxinsns2(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len; i++) insn[i] = __BPF_STMT(BPF_RET | BPF_K, 0xfefefefe); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int bpf_fill_maxinsns3(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; struct rnd_state rnd; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; prandom_seed_state(&rnd, 3141592653589793238ULL); for (i = 0; i < len - 1; i++) { __u32 k = prandom_u32_state(&rnd); insn[i] = __BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, k); } insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int bpf_fill_maxinsns4(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS + 1; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len; i++) insn[i] = __BPF_STMT(BPF_RET | BPF_K, 0xfefefefe); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int bpf_fill_maxinsns5(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[0] = __BPF_JUMP(BPF_JMP | BPF_JA, len - 2, 0, 0); for (i = 1; i < len - 1; i++) insn[i] = __BPF_STMT(BPF_RET | BPF_K, 0xfefefefe); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_K, 0xabababab); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int bpf_fill_maxinsns6(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len - 1; i++) insn[i] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS, SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int bpf_fill_maxinsns7(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len - 4; i++) insn[i] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS, SKF_AD_OFF + SKF_AD_CPU); insn[len - 4] = __BPF_STMT(BPF_MISC | BPF_TAX, 0); insn[len - 3] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS, SKF_AD_OFF + SKF_AD_CPU); insn[len - 2] = __BPF_STMT(BPF_ALU | BPF_SUB | BPF_X, 0); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int bpf_fill_maxinsns8(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i, jmp_off = len - 3; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[0] = __BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff); for (i = 1; i < len - 1; i++) insn[i] = __BPF_JUMP(BPF_JMP | BPF_JGT, 0xffffffff, jmp_off--, 0); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int bpf_fill_maxinsns9(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct bpf_insn *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[0] = BPF_JMP_IMM(BPF_JA, 0, 0, len - 2); insn[1] = BPF_ALU32_IMM(BPF_MOV, R0, 0xcbababab); insn[2] = BPF_EXIT_INSN(); for (i = 3; i < len - 2; i++) insn[i] = BPF_ALU32_IMM(BPF_MOV, R0, 0xfefefefe); insn[len - 2] = BPF_EXIT_INSN(); insn[len - 1] = BPF_JMP_IMM(BPF_JA, 0, 0, -(len - 1)); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int bpf_fill_maxinsns10(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS, hlen = len - 2; struct bpf_insn *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < hlen / 2; i++) insn[i] = BPF_JMP_IMM(BPF_JA, 0, 0, hlen - 2 - 2 * i); for (i = hlen - 1; i > hlen / 2; i--) insn[i] = BPF_JMP_IMM(BPF_JA, 0, 0, hlen - 1 - 2 * i); insn[hlen / 2] = BPF_JMP_IMM(BPF_JA, 0, 0, hlen / 2 - 1); insn[hlen] = BPF_ALU32_IMM(BPF_MOV, R0, 0xabababac); insn[hlen + 1] = BPF_EXIT_INSN(); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int __bpf_fill_ja(struct bpf_test *self, unsigned int len, unsigned int plen) { struct sock_filter *insn; unsigned int rlen; int i, j; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; rlen = (len % plen) - 1; for (i = 0; i + plen < len; i += plen) for (j = 0; j < plen; j++) insn[i + j] = __BPF_JUMP(BPF_JMP | BPF_JA, plen - 1 - j, 0, 0); for (j = 0; j < rlen; j++) insn[i + j] = __BPF_JUMP(BPF_JMP | BPF_JA, rlen - 1 - j, 0, 0); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_K, 0xababcbac); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int bpf_fill_maxinsns11(struct bpf_test *self) { /* Hits 70 passes on x86_64, so cannot get JITed there. */ return __bpf_fill_ja(self, BPF_MAXINSNS, 68); } static int bpf_fill_ja(struct bpf_test *self) { /* Hits exactly 11 passes on x86_64 JIT. */ return __bpf_fill_ja(self, 12, 9); } static int bpf_fill_ld_abs_get_processor_id(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len - 1; i += 2) { insn[i] = __BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 0); insn[i + 1] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS, SKF_AD_OFF + SKF_AD_CPU); } insn[len - 1] = __BPF_STMT(BPF_RET | BPF_K, 0xbee); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } #define PUSH_CNT 68 /* test: {skb->data[0], vlan_push} x 68 + {skb->data[0], vlan_pop} x 68 */ static int bpf_fill_ld_abs_vlan_push_pop(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct bpf_insn *insn; int i = 0, j, k = 0; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[i++] = BPF_MOV64_REG(R6, R1); loop: for (j = 0; j < PUSH_CNT; j++) { insn[i++] = BPF_LD_ABS(BPF_B, 0); insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0x34, len - i - 2); i++; insn[i++] = BPF_MOV64_REG(R1, R6); insn[i++] = BPF_MOV64_IMM(R2, 1); insn[i++] = BPF_MOV64_IMM(R3, 2); insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, bpf_skb_vlan_push_proto.func - __bpf_call_base); insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0, len - i - 2); i++; } for (j = 0; j < PUSH_CNT; j++) { insn[i++] = BPF_LD_ABS(BPF_B, 0); insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0x34, len - i - 2); i++; insn[i++] = BPF_MOV64_REG(R1, R6); insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, bpf_skb_vlan_pop_proto.func - __bpf_call_base); insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0, len - i - 2); i++; } if (++k < 5) goto loop; for (; i < len - 1; i++) insn[i] = BPF_ALU32_IMM(BPF_MOV, R0, 0xbef); insn[len - 1] = BPF_EXIT_INSN(); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int bpf_fill_jump_around_ld_abs(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct bpf_insn *insn; int i = 0; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[i++] = BPF_MOV64_REG(R6, R1); insn[i++] = BPF_LD_ABS(BPF_B, 0); insn[i] = BPF_JMP_IMM(BPF_JEQ, R0, 10, len - i - 2); i++; while (i < len - 1) insn[i++] = BPF_LD_ABS(BPF_B, 1); insn[i] = BPF_EXIT_INSN(); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; } static int __bpf_fill_stxdw(struct bpf_test *self, int size) { unsigned int len = BPF_MAXINSNS; struct bpf_insn *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[0] = BPF_ALU32_IMM(BPF_MOV, R0, 1); insn[1] = BPF_ST_MEM(size, R10, -40, 42); for (i = 2; i < len - 2; i++) insn[i] = BPF_STX_XADD(size, R10, R0, -40); insn[len - 2] = BPF_LDX_MEM(size, R0, R10, -40); insn[len - 1] = BPF_EXIT_INSN(); self->u.ptr.insns = insn; self->u.ptr.len = len; self->stack_depth = 40; return 0; } static int bpf_fill_stxw(struct bpf_test *self) { return __bpf_fill_stxdw(self, BPF_W); } static int bpf_fill_stxdw(struct bpf_test *self) { return __bpf_fill_stxdw(self, BPF_DW); } static struct bpf_test tests[] = { { "TAX",