xref: /openbmc/linux/arch/x86/net/bpf_jit_comp.c (revision d00a9dd2)
10a14842fSEric Dumazet /* bpf_jit_comp.c : BPF JIT compiler
20a14842fSEric Dumazet  *
30a14842fSEric Dumazet  * Copyright (C) 2011 Eric Dumazet (eric.dumazet@gmail.com)
40a14842fSEric Dumazet  *
50a14842fSEric Dumazet  * This program is free software; you can redistribute it and/or
60a14842fSEric Dumazet  * modify it under the terms of the GNU General Public License
70a14842fSEric Dumazet  * as published by the Free Software Foundation; version 2
80a14842fSEric Dumazet  * of the License.
90a14842fSEric Dumazet  */
100a14842fSEric Dumazet #include <linux/moduleloader.h>
110a14842fSEric Dumazet #include <asm/cacheflush.h>
120a14842fSEric Dumazet #include <linux/netdevice.h>
130a14842fSEric Dumazet #include <linux/filter.h>
140a14842fSEric Dumazet 
150a14842fSEric Dumazet /*
160a14842fSEric Dumazet  * Conventions :
170a14842fSEric Dumazet  *  EAX : BPF A accumulator
180a14842fSEric Dumazet  *  EBX : BPF X accumulator
190a14842fSEric Dumazet  *  RDI : pointer to skb   (first argument given to JIT function)
200a14842fSEric Dumazet  *  RBP : frame pointer (even if CONFIG_FRAME_POINTER=n)
210a14842fSEric Dumazet  *  ECX,EDX,ESI : scratch registers
220a14842fSEric Dumazet  *  r9d : skb->len - skb->data_len (headlen)
230a14842fSEric Dumazet  *  r8  : skb->data
240a14842fSEric Dumazet  * -8(RBP) : saved RBX value
250a14842fSEric Dumazet  * -16(RBP)..-80(RBP) : BPF_MEMWORDS values
260a14842fSEric Dumazet  */
270a14842fSEric Dumazet int bpf_jit_enable __read_mostly;
280a14842fSEric Dumazet 
290a14842fSEric Dumazet /*
300a14842fSEric Dumazet  * assembly code in arch/x86/net/bpf_jit.S
310a14842fSEric Dumazet  */
320a14842fSEric Dumazet extern u8 sk_load_word[], sk_load_half[], sk_load_byte[], sk_load_byte_msh[];
330a14842fSEric Dumazet extern u8 sk_load_word_ind[], sk_load_half_ind[], sk_load_byte_ind[];
340a14842fSEric Dumazet 
350a14842fSEric Dumazet static inline u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
360a14842fSEric Dumazet {
370a14842fSEric Dumazet 	if (len == 1)
380a14842fSEric Dumazet 		*ptr = bytes;
390a14842fSEric Dumazet 	else if (len == 2)
400a14842fSEric Dumazet 		*(u16 *)ptr = bytes;
410a14842fSEric Dumazet 	else {
420a14842fSEric Dumazet 		*(u32 *)ptr = bytes;
430a14842fSEric Dumazet 		barrier();
440a14842fSEric Dumazet 	}
450a14842fSEric Dumazet 	return ptr + len;
460a14842fSEric Dumazet }
470a14842fSEric Dumazet 
480a14842fSEric Dumazet #define EMIT(bytes, len)	do { prog = emit_code(prog, bytes, len); } while (0)
490a14842fSEric Dumazet 
500a14842fSEric Dumazet #define EMIT1(b1)		EMIT(b1, 1)
510a14842fSEric Dumazet #define EMIT2(b1, b2)		EMIT((b1) + ((b2) << 8), 2)
520a14842fSEric Dumazet #define EMIT3(b1, b2, b3)	EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
530a14842fSEric Dumazet #define EMIT4(b1, b2, b3, b4)   EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
540a14842fSEric Dumazet #define EMIT1_off32(b1, off)	do { EMIT1(b1); EMIT(off, 4);} while (0)
550a14842fSEric Dumazet 
560a14842fSEric Dumazet #define CLEAR_A() EMIT2(0x31, 0xc0) /* xor %eax,%eax */
570a14842fSEric Dumazet #define CLEAR_X() EMIT2(0x31, 0xdb) /* xor %ebx,%ebx */
580a14842fSEric Dumazet 
590a14842fSEric Dumazet static inline bool is_imm8(int value)
600a14842fSEric Dumazet {
610a14842fSEric Dumazet 	return value <= 127 && value >= -128;
620a14842fSEric Dumazet }
630a14842fSEric Dumazet 
640a14842fSEric Dumazet static inline bool is_near(int offset)
650a14842fSEric Dumazet {
660a14842fSEric Dumazet 	return offset <= 127 && offset >= -128;
670a14842fSEric Dumazet }
680a14842fSEric Dumazet 
690a14842fSEric Dumazet #define EMIT_JMP(offset)						\
700a14842fSEric Dumazet do {									\
710a14842fSEric Dumazet 	if (offset) {							\
720a14842fSEric Dumazet 		if (is_near(offset))					\
730a14842fSEric Dumazet 			EMIT2(0xeb, offset); /* jmp .+off8 */		\
740a14842fSEric Dumazet 		else							\
750a14842fSEric Dumazet 			EMIT1_off32(0xe9, offset); /* jmp .+off32 */	\
760a14842fSEric Dumazet 	}								\
770a14842fSEric Dumazet } while (0)
780a14842fSEric Dumazet 
790a14842fSEric Dumazet /* list of x86 cond jumps opcodes (. + s8)
800a14842fSEric Dumazet  * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
810a14842fSEric Dumazet  */
820a14842fSEric Dumazet #define X86_JB  0x72
830a14842fSEric Dumazet #define X86_JAE 0x73
840a14842fSEric Dumazet #define X86_JE  0x74
850a14842fSEric Dumazet #define X86_JNE 0x75
860a14842fSEric Dumazet #define X86_JBE 0x76
870a14842fSEric Dumazet #define X86_JA  0x77
880a14842fSEric Dumazet 
890a14842fSEric Dumazet #define EMIT_COND_JMP(op, offset)				\
900a14842fSEric Dumazet do {								\
910a14842fSEric Dumazet 	if (is_near(offset))					\
920a14842fSEric Dumazet 		EMIT2(op, offset); /* jxx .+off8 */		\
930a14842fSEric Dumazet 	else {							\
940a14842fSEric Dumazet 		EMIT2(0x0f, op + 0x10);				\
950a14842fSEric Dumazet 		EMIT(offset, 4); /* jxx .+off32 */		\
960a14842fSEric Dumazet 	}							\
970a14842fSEric Dumazet } while (0)
980a14842fSEric Dumazet 
990a14842fSEric Dumazet #define COND_SEL(CODE, TOP, FOP)	\
1000a14842fSEric Dumazet 	case CODE:			\
1010a14842fSEric Dumazet 		t_op = TOP;		\
1020a14842fSEric Dumazet 		f_op = FOP;		\
1030a14842fSEric Dumazet 		goto cond_branch
1040a14842fSEric Dumazet 
1050a14842fSEric Dumazet 
1060a14842fSEric Dumazet #define SEEN_DATAREF 1 /* might call external helpers */
1070a14842fSEric Dumazet #define SEEN_XREG    2 /* ebx is used */
1080a14842fSEric Dumazet #define SEEN_MEM     4 /* use mem[] for temporary storage */
1090a14842fSEric Dumazet 
1100a14842fSEric Dumazet static inline void bpf_flush_icache(void *start, void *end)
1110a14842fSEric Dumazet {
1120a14842fSEric Dumazet 	mm_segment_t old_fs = get_fs();
1130a14842fSEric Dumazet 
1140a14842fSEric Dumazet 	set_fs(KERNEL_DS);
1150a14842fSEric Dumazet 	smp_wmb();
1160a14842fSEric Dumazet 	flush_icache_range((unsigned long)start, (unsigned long)end);
1170a14842fSEric Dumazet 	set_fs(old_fs);
1180a14842fSEric Dumazet }
1190a14842fSEric Dumazet 
1200a14842fSEric Dumazet 
1210a14842fSEric Dumazet void bpf_jit_compile(struct sk_filter *fp)
1220a14842fSEric Dumazet {
1230a14842fSEric Dumazet 	u8 temp[64];
1240a14842fSEric Dumazet 	u8 *prog;
1250a14842fSEric Dumazet 	unsigned int proglen, oldproglen = 0;
1260a14842fSEric Dumazet 	int ilen, i;
1270a14842fSEric Dumazet 	int t_offset, f_offset;
1280a14842fSEric Dumazet 	u8 t_op, f_op, seen = 0, pass;
1290a14842fSEric Dumazet 	u8 *image = NULL;
1300a14842fSEric Dumazet 	u8 *func;
1310a14842fSEric Dumazet 	int pc_ret0 = -1; /* bpf index of first RET #0 instruction (if any) */
1320a14842fSEric Dumazet 	unsigned int cleanup_addr; /* epilogue code offset */
1330a14842fSEric Dumazet 	unsigned int *addrs;
1340a14842fSEric Dumazet 	const struct sock_filter *filter = fp->insns;
1350a14842fSEric Dumazet 	int flen = fp->len;
1360a14842fSEric Dumazet 
1370a14842fSEric Dumazet 	if (!bpf_jit_enable)
1380a14842fSEric Dumazet 		return;
1390a14842fSEric Dumazet 
1400a14842fSEric Dumazet 	addrs = kmalloc(flen * sizeof(*addrs), GFP_KERNEL);
1410a14842fSEric Dumazet 	if (addrs == NULL)
1420a14842fSEric Dumazet 		return;
1430a14842fSEric Dumazet 
1440a14842fSEric Dumazet 	/* Before first pass, make a rough estimation of addrs[]
1450a14842fSEric Dumazet 	 * each bpf instruction is translated to less than 64 bytes
1460a14842fSEric Dumazet 	 */
1470a14842fSEric Dumazet 	for (proglen = 0, i = 0; i < flen; i++) {
1480a14842fSEric Dumazet 		proglen += 64;
1490a14842fSEric Dumazet 		addrs[i] = proglen;
1500a14842fSEric Dumazet 	}
1510a14842fSEric Dumazet 	cleanup_addr = proglen; /* epilogue address */
1520a14842fSEric Dumazet 
1530a14842fSEric Dumazet 	for (pass = 0; pass < 10; pass++) {
154d00a9dd2SEric Dumazet 		u8 seen_or_pass0 = (pass == 0) ? (SEEN_XREG | SEEN_DATAREF | SEEN_MEM) : seen;
1550a14842fSEric Dumazet 		/* no prologue/epilogue for trivial filters (RET something) */
1560a14842fSEric Dumazet 		proglen = 0;
1570a14842fSEric Dumazet 		prog = temp;
1580a14842fSEric Dumazet 
159d00a9dd2SEric Dumazet 		if (seen_or_pass0) {
1600a14842fSEric Dumazet 			EMIT4(0x55, 0x48, 0x89, 0xe5); /* push %rbp; mov %rsp,%rbp */
1610a14842fSEric Dumazet 			EMIT4(0x48, 0x83, 0xec, 96);	/* subq  $96,%rsp	*/
1620a14842fSEric Dumazet 			/* note : must save %rbx in case bpf_error is hit */
163d00a9dd2SEric Dumazet 			if (seen_or_pass0 & (SEEN_XREG | SEEN_DATAREF))
1640a14842fSEric Dumazet 				EMIT4(0x48, 0x89, 0x5d, 0xf8); /* mov %rbx, -8(%rbp) */
165d00a9dd2SEric Dumazet 			if (seen_or_pass0 & SEEN_XREG)
1660a14842fSEric Dumazet 				CLEAR_X(); /* make sure we dont leek kernel memory */
1670a14842fSEric Dumazet 
1680a14842fSEric Dumazet 			/*
1690a14842fSEric Dumazet 			 * If this filter needs to access skb data,
1700a14842fSEric Dumazet 			 * loads r9 and r8 with :
1710a14842fSEric Dumazet 			 *  r9 = skb->len - skb->data_len
1720a14842fSEric Dumazet 			 *  r8 = skb->data
1730a14842fSEric Dumazet 			 */
174d00a9dd2SEric Dumazet 			if (seen_or_pass0 & SEEN_DATAREF) {
1750a14842fSEric Dumazet 				if (offsetof(struct sk_buff, len) <= 127)
1760a14842fSEric Dumazet 					/* mov    off8(%rdi),%r9d */
1770a14842fSEric Dumazet 					EMIT4(0x44, 0x8b, 0x4f, offsetof(struct sk_buff, len));
1780a14842fSEric Dumazet 				else {
1790a14842fSEric Dumazet 					/* mov    off32(%rdi),%r9d */
1800a14842fSEric Dumazet 					EMIT3(0x44, 0x8b, 0x8f);
1810a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, len), 4);
1820a14842fSEric Dumazet 				}
1830a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, data_len)))
1840a14842fSEric Dumazet 					/* sub    off8(%rdi),%r9d */
1850a14842fSEric Dumazet 					EMIT4(0x44, 0x2b, 0x4f, offsetof(struct sk_buff, data_len));
1860a14842fSEric Dumazet 				else {
1870a14842fSEric Dumazet 					EMIT3(0x44, 0x2b, 0x8f);
1880a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, data_len), 4);
1890a14842fSEric Dumazet 				}
1900a14842fSEric Dumazet 
1910a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, data)))
1920a14842fSEric Dumazet 					/* mov off8(%rdi),%r8 */
1930a14842fSEric Dumazet 					EMIT4(0x4c, 0x8b, 0x47, offsetof(struct sk_buff, data));
1940a14842fSEric Dumazet 				else {
1950a14842fSEric Dumazet 					/* mov off32(%rdi),%r8 */
1960a14842fSEric Dumazet 					EMIT3(0x4c, 0x8b, 0x87);
1970a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, data), 4);
1980a14842fSEric Dumazet 				}
1990a14842fSEric Dumazet 			}
2000a14842fSEric Dumazet 		}
2010a14842fSEric Dumazet 
2020a14842fSEric Dumazet 		switch (filter[0].code) {
2030a14842fSEric Dumazet 		case BPF_S_RET_K:
2040a14842fSEric Dumazet 		case BPF_S_LD_W_LEN:
2050a14842fSEric Dumazet 		case BPF_S_ANC_PROTOCOL:
2060a14842fSEric Dumazet 		case BPF_S_ANC_IFINDEX:
2070a14842fSEric Dumazet 		case BPF_S_ANC_MARK:
2080a14842fSEric Dumazet 		case BPF_S_ANC_RXHASH:
2090a14842fSEric Dumazet 		case BPF_S_ANC_CPU:
2100a14842fSEric Dumazet 		case BPF_S_ANC_QUEUE:
2110a14842fSEric Dumazet 		case BPF_S_LD_W_ABS:
2120a14842fSEric Dumazet 		case BPF_S_LD_H_ABS:
2130a14842fSEric Dumazet 		case BPF_S_LD_B_ABS:
2140a14842fSEric Dumazet 			/* first instruction sets A register (or is RET 'constant') */
2150a14842fSEric Dumazet 			break;
2160a14842fSEric Dumazet 		default:
2170a14842fSEric Dumazet 			/* make sure we dont leak kernel information to user */
2180a14842fSEric Dumazet 			CLEAR_A(); /* A = 0 */
2190a14842fSEric Dumazet 		}
2200a14842fSEric Dumazet 
2210a14842fSEric Dumazet 		for (i = 0; i < flen; i++) {
2220a14842fSEric Dumazet 			unsigned int K = filter[i].k;
2230a14842fSEric Dumazet 
2240a14842fSEric Dumazet 			switch (filter[i].code) {
2250a14842fSEric Dumazet 			case BPF_S_ALU_ADD_X: /* A += X; */
2260a14842fSEric Dumazet 				seen |= SEEN_XREG;
2270a14842fSEric Dumazet 				EMIT2(0x01, 0xd8);		/* add %ebx,%eax */
2280a14842fSEric Dumazet 				break;
2290a14842fSEric Dumazet 			case BPF_S_ALU_ADD_K: /* A += K; */
2300a14842fSEric Dumazet 				if (!K)
2310a14842fSEric Dumazet 					break;
2320a14842fSEric Dumazet 				if (is_imm8(K))
2330a14842fSEric Dumazet 					EMIT3(0x83, 0xc0, K);	/* add imm8,%eax */
2340a14842fSEric Dumazet 				else
2350a14842fSEric Dumazet 					EMIT1_off32(0x05, K);	/* add imm32,%eax */
2360a14842fSEric Dumazet 				break;
2370a14842fSEric Dumazet 			case BPF_S_ALU_SUB_X: /* A -= X; */
2380a14842fSEric Dumazet 				seen |= SEEN_XREG;
2390a14842fSEric Dumazet 				EMIT2(0x29, 0xd8);		/* sub    %ebx,%eax */
2400a14842fSEric Dumazet 				break;
2410a14842fSEric Dumazet 			case BPF_S_ALU_SUB_K: /* A -= K */
2420a14842fSEric Dumazet 				if (!K)
2430a14842fSEric Dumazet 					break;
2440a14842fSEric Dumazet 				if (is_imm8(K))
2450a14842fSEric Dumazet 					EMIT3(0x83, 0xe8, K); /* sub imm8,%eax */
2460a14842fSEric Dumazet 				else
2470a14842fSEric Dumazet 					EMIT1_off32(0x2d, K); /* sub imm32,%eax */
2480a14842fSEric Dumazet 				break;
2490a14842fSEric Dumazet 			case BPF_S_ALU_MUL_X: /* A *= X; */
2500a14842fSEric Dumazet 				seen |= SEEN_XREG;
2510a14842fSEric Dumazet 				EMIT3(0x0f, 0xaf, 0xc3);	/* imul %ebx,%eax */
2520a14842fSEric Dumazet 				break;
2530a14842fSEric Dumazet 			case BPF_S_ALU_MUL_K: /* A *= K */
2540a14842fSEric Dumazet 				if (is_imm8(K))
2550a14842fSEric Dumazet 					EMIT3(0x6b, 0xc0, K); /* imul imm8,%eax,%eax */
2560a14842fSEric Dumazet 				else {
2570a14842fSEric Dumazet 					EMIT2(0x69, 0xc0);		/* imul imm32,%eax */
2580a14842fSEric Dumazet 					EMIT(K, 4);
2590a14842fSEric Dumazet 				}
2600a14842fSEric Dumazet 				break;
2610a14842fSEric Dumazet 			case BPF_S_ALU_DIV_X: /* A /= X; */
2620a14842fSEric Dumazet 				seen |= SEEN_XREG;
2630a14842fSEric Dumazet 				EMIT2(0x85, 0xdb);	/* test %ebx,%ebx */
264d00a9dd2SEric Dumazet 				if (pc_ret0 > 0) {
265d00a9dd2SEric Dumazet 					/* addrs[pc_ret0 - 1] is start address of target
266d00a9dd2SEric Dumazet 					 * (addrs[i] - 4) is the address following this jmp
267d00a9dd2SEric Dumazet 					 * ("xor %edx,%edx; div %ebx" being 4 bytes long)
268d00a9dd2SEric Dumazet 					 */
269d00a9dd2SEric Dumazet 					EMIT_COND_JMP(X86_JE, addrs[pc_ret0 - 1] -
270d00a9dd2SEric Dumazet 								(addrs[i] - 4));
271d00a9dd2SEric Dumazet 				} else {
2720a14842fSEric Dumazet 					EMIT_COND_JMP(X86_JNE, 2 + 5);
2730a14842fSEric Dumazet 					CLEAR_A();
2740a14842fSEric Dumazet 					EMIT1_off32(0xe9, cleanup_addr - (addrs[i] - 4)); /* jmp .+off32 */
2750a14842fSEric Dumazet 				}
2760a14842fSEric Dumazet 				EMIT4(0x31, 0xd2, 0xf7, 0xf3); /* xor %edx,%edx; div %ebx */
2770a14842fSEric Dumazet 				break;
2780a14842fSEric Dumazet 			case BPF_S_ALU_DIV_K: /* A = reciprocal_divide(A, K); */
2790a14842fSEric Dumazet 				EMIT3(0x48, 0x69, 0xc0); /* imul imm32,%rax,%rax */
2800a14842fSEric Dumazet 				EMIT(K, 4);
2810a14842fSEric Dumazet 				EMIT4(0x48, 0xc1, 0xe8, 0x20); /* shr $0x20,%rax */
2820a14842fSEric Dumazet 				break;
2830a14842fSEric Dumazet 			case BPF_S_ALU_AND_X:
2840a14842fSEric Dumazet 				seen |= SEEN_XREG;
2850a14842fSEric Dumazet 				EMIT2(0x21, 0xd8);		/* and %ebx,%eax */
2860a14842fSEric Dumazet 				break;
2870a14842fSEric Dumazet 			case BPF_S_ALU_AND_K:
2880a14842fSEric Dumazet 				if (K >= 0xFFFFFF00) {
2890a14842fSEric Dumazet 					EMIT2(0x24, K & 0xFF); /* and imm8,%al */
2900a14842fSEric Dumazet 				} else if (K >= 0xFFFF0000) {
2910a14842fSEric Dumazet 					EMIT2(0x66, 0x25);	/* and imm16,%ax */
2920a14842fSEric Dumazet 					EMIT2(K, 2);
2930a14842fSEric Dumazet 				} else {
2940a14842fSEric Dumazet 					EMIT1_off32(0x25, K);	/* and imm32,%eax */
2950a14842fSEric Dumazet 				}
2960a14842fSEric Dumazet 				break;
2970a14842fSEric Dumazet 			case BPF_S_ALU_OR_X:
2980a14842fSEric Dumazet 				seen |= SEEN_XREG;
2990a14842fSEric Dumazet 				EMIT2(0x09, 0xd8);		/* or %ebx,%eax */
3000a14842fSEric Dumazet 				break;
3010a14842fSEric Dumazet 			case BPF_S_ALU_OR_K:
3020a14842fSEric Dumazet 				if (is_imm8(K))
3030a14842fSEric Dumazet 					EMIT3(0x83, 0xc8, K); /* or imm8,%eax */
3040a14842fSEric Dumazet 				else
3050a14842fSEric Dumazet 					EMIT1_off32(0x0d, K);	/* or imm32,%eax */
3060a14842fSEric Dumazet 				break;
3070a14842fSEric Dumazet 			case BPF_S_ALU_LSH_X: /* A <<= X; */
3080a14842fSEric Dumazet 				seen |= SEEN_XREG;
3090a14842fSEric Dumazet 				EMIT4(0x89, 0xd9, 0xd3, 0xe0);	/* mov %ebx,%ecx; shl %cl,%eax */
3100a14842fSEric Dumazet 				break;
3110a14842fSEric Dumazet 			case BPF_S_ALU_LSH_K:
3120a14842fSEric Dumazet 				if (K == 0)
3130a14842fSEric Dumazet 					break;
3140a14842fSEric Dumazet 				else if (K == 1)
3150a14842fSEric Dumazet 					EMIT2(0xd1, 0xe0); /* shl %eax */
3160a14842fSEric Dumazet 				else
3170a14842fSEric Dumazet 					EMIT3(0xc1, 0xe0, K);
3180a14842fSEric Dumazet 				break;
3190a14842fSEric Dumazet 			case BPF_S_ALU_RSH_X: /* A >>= X; */
3200a14842fSEric Dumazet 				seen |= SEEN_XREG;
3210a14842fSEric Dumazet 				EMIT4(0x89, 0xd9, 0xd3, 0xe8);	/* mov %ebx,%ecx; shr %cl,%eax */
3220a14842fSEric Dumazet 				break;
3230a14842fSEric Dumazet 			case BPF_S_ALU_RSH_K: /* A >>= K; */
3240a14842fSEric Dumazet 				if (K == 0)
3250a14842fSEric Dumazet 					break;
3260a14842fSEric Dumazet 				else if (K == 1)
3270a14842fSEric Dumazet 					EMIT2(0xd1, 0xe8); /* shr %eax */
3280a14842fSEric Dumazet 				else
3290a14842fSEric Dumazet 					EMIT3(0xc1, 0xe8, K);
3300a14842fSEric Dumazet 				break;
3310a14842fSEric Dumazet 			case BPF_S_ALU_NEG:
3320a14842fSEric Dumazet 				EMIT2(0xf7, 0xd8);		/* neg %eax */
3330a14842fSEric Dumazet 				break;
3340a14842fSEric Dumazet 			case BPF_S_RET_K:
3350a14842fSEric Dumazet 				if (!K) {
3360a14842fSEric Dumazet 					if (pc_ret0 == -1)
3370a14842fSEric Dumazet 						pc_ret0 = i;
3380a14842fSEric Dumazet 					CLEAR_A();
3390a14842fSEric Dumazet 				} else {
3400a14842fSEric Dumazet 					EMIT1_off32(0xb8, K);	/* mov $imm32,%eax */
3410a14842fSEric Dumazet 				}
3420a14842fSEric Dumazet 				/* fallinto */
3430a14842fSEric Dumazet 			case BPF_S_RET_A:
344d00a9dd2SEric Dumazet 				if (seen_or_pass0) {
3450a14842fSEric Dumazet 					if (i != flen - 1) {
3460a14842fSEric Dumazet 						EMIT_JMP(cleanup_addr - addrs[i]);
3470a14842fSEric Dumazet 						break;
3480a14842fSEric Dumazet 					}
349d00a9dd2SEric Dumazet 					if (seen_or_pass0 & SEEN_XREG)
3500a14842fSEric Dumazet 						EMIT4(0x48, 0x8b, 0x5d, 0xf8);  /* mov  -8(%rbp),%rbx */
3510a14842fSEric Dumazet 					EMIT1(0xc9);		/* leaveq */
3520a14842fSEric Dumazet 				}
3530a14842fSEric Dumazet 				EMIT1(0xc3);		/* ret */
3540a14842fSEric Dumazet 				break;
3550a14842fSEric Dumazet 			case BPF_S_MISC_TAX: /* X = A */
3560a14842fSEric Dumazet 				seen |= SEEN_XREG;
3570a14842fSEric Dumazet 				EMIT2(0x89, 0xc3);	/* mov    %eax,%ebx */
3580a14842fSEric Dumazet 				break;
3590a14842fSEric Dumazet 			case BPF_S_MISC_TXA: /* A = X */
3600a14842fSEric Dumazet 				seen |= SEEN_XREG;
3610a14842fSEric Dumazet 				EMIT2(0x89, 0xd8);	/* mov    %ebx,%eax */
3620a14842fSEric Dumazet 				break;
3630a14842fSEric Dumazet 			case BPF_S_LD_IMM: /* A = K */
3640a14842fSEric Dumazet 				if (!K)
3650a14842fSEric Dumazet 					CLEAR_A();
3660a14842fSEric Dumazet 				else
3670a14842fSEric Dumazet 					EMIT1_off32(0xb8, K); /* mov $imm32,%eax */
3680a14842fSEric Dumazet 				break;
3690a14842fSEric Dumazet 			case BPF_S_LDX_IMM: /* X = K */
3700a14842fSEric Dumazet 				seen |= SEEN_XREG;
3710a14842fSEric Dumazet 				if (!K)
3720a14842fSEric Dumazet 					CLEAR_X();
3730a14842fSEric Dumazet 				else
3740a14842fSEric Dumazet 					EMIT1_off32(0xbb, K); /* mov $imm32,%ebx */
3750a14842fSEric Dumazet 				break;
3760a14842fSEric Dumazet 			case BPF_S_LD_MEM: /* A = mem[K] : mov off8(%rbp),%eax */
3770a14842fSEric Dumazet 				seen |= SEEN_MEM;
3780a14842fSEric Dumazet 				EMIT3(0x8b, 0x45, 0xf0 - K*4);
3790a14842fSEric Dumazet 				break;
3800a14842fSEric Dumazet 			case BPF_S_LDX_MEM: /* X = mem[K] : mov off8(%rbp),%ebx */
3810a14842fSEric Dumazet 				seen |= SEEN_XREG | SEEN_MEM;
3820a14842fSEric Dumazet 				EMIT3(0x8b, 0x5d, 0xf0 - K*4);
3830a14842fSEric Dumazet 				break;
3840a14842fSEric Dumazet 			case BPF_S_ST: /* mem[K] = A : mov %eax,off8(%rbp) */
3850a14842fSEric Dumazet 				seen |= SEEN_MEM;
3860a14842fSEric Dumazet 				EMIT3(0x89, 0x45, 0xf0 - K*4);
3870a14842fSEric Dumazet 				break;
3880a14842fSEric Dumazet 			case BPF_S_STX: /* mem[K] = X : mov %ebx,off8(%rbp) */
3890a14842fSEric Dumazet 				seen |= SEEN_XREG | SEEN_MEM;
3900a14842fSEric Dumazet 				EMIT3(0x89, 0x5d, 0xf0 - K*4);
3910a14842fSEric Dumazet 				break;
3920a14842fSEric Dumazet 			case BPF_S_LD_W_LEN: /*	A = skb->len; */
3930a14842fSEric Dumazet 				BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
3940a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, len)))
3950a14842fSEric Dumazet 					/* mov    off8(%rdi),%eax */
3960a14842fSEric Dumazet 					EMIT3(0x8b, 0x47, offsetof(struct sk_buff, len));
3970a14842fSEric Dumazet 				else {
3980a14842fSEric Dumazet 					EMIT2(0x8b, 0x87);
3990a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, len), 4);
4000a14842fSEric Dumazet 				}
4010a14842fSEric Dumazet 				break;
4020a14842fSEric Dumazet 			case BPF_S_LDX_W_LEN: /* X = skb->len; */
4030a14842fSEric Dumazet 				seen |= SEEN_XREG;
4040a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, len)))
4050a14842fSEric Dumazet 					/* mov off8(%rdi),%ebx */
4060a14842fSEric Dumazet 					EMIT3(0x8b, 0x5f, offsetof(struct sk_buff, len));
4070a14842fSEric Dumazet 				else {
4080a14842fSEric Dumazet 					EMIT2(0x8b, 0x9f);
4090a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, len), 4);
4100a14842fSEric Dumazet 				}
4110a14842fSEric Dumazet 				break;
4120a14842fSEric Dumazet 			case BPF_S_ANC_PROTOCOL: /* A = ntohs(skb->protocol); */
4130a14842fSEric Dumazet 				BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
4140a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, protocol))) {
4150a14842fSEric Dumazet 					/* movzwl off8(%rdi),%eax */
4160a14842fSEric Dumazet 					EMIT4(0x0f, 0xb7, 0x47, offsetof(struct sk_buff, protocol));
4170a14842fSEric Dumazet 				} else {
4180a14842fSEric Dumazet 					EMIT3(0x0f, 0xb7, 0x87); /* movzwl off32(%rdi),%eax */
4190a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, protocol), 4);
4200a14842fSEric Dumazet 				}
4210a14842fSEric Dumazet 				EMIT2(0x86, 0xc4); /* ntohs() : xchg   %al,%ah */
4220a14842fSEric Dumazet 				break;
4230a14842fSEric Dumazet 			case BPF_S_ANC_IFINDEX:
4240a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, dev))) {
4250a14842fSEric Dumazet 					/* movq off8(%rdi),%rax */
4260a14842fSEric Dumazet 					EMIT4(0x48, 0x8b, 0x47, offsetof(struct sk_buff, dev));
4270a14842fSEric Dumazet 				} else {
4280a14842fSEric Dumazet 					EMIT3(0x48, 0x8b, 0x87); /* movq off32(%rdi),%rax */
4290a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, dev), 4);
4300a14842fSEric Dumazet 				}
4310a14842fSEric Dumazet 				EMIT3(0x48, 0x85, 0xc0);	/* test %rax,%rax */
4320a14842fSEric Dumazet 				EMIT_COND_JMP(X86_JE, cleanup_addr - (addrs[i] - 6));
4330a14842fSEric Dumazet 				BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
4340a14842fSEric Dumazet 				EMIT2(0x8b, 0x80);	/* mov off32(%rax),%eax */
4350a14842fSEric Dumazet 				EMIT(offsetof(struct net_device, ifindex), 4);
4360a14842fSEric Dumazet 				break;
4370a14842fSEric Dumazet 			case BPF_S_ANC_MARK:
4380a14842fSEric Dumazet 				BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
4390a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, mark))) {
4400a14842fSEric Dumazet 					/* mov off8(%rdi),%eax */
4410a14842fSEric Dumazet 					EMIT3(0x8b, 0x47, offsetof(struct sk_buff, mark));
4420a14842fSEric Dumazet 				} else {
4430a14842fSEric Dumazet 					EMIT2(0x8b, 0x87);
4440a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, mark), 4);
4450a14842fSEric Dumazet 				}
4460a14842fSEric Dumazet 				break;
4470a14842fSEric Dumazet 			case BPF_S_ANC_RXHASH:
4480a14842fSEric Dumazet 				BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, rxhash) != 4);
4490a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, rxhash))) {
4500a14842fSEric Dumazet 					/* mov off8(%rdi),%eax */
4510a14842fSEric Dumazet 					EMIT3(0x8b, 0x47, offsetof(struct sk_buff, rxhash));
4520a14842fSEric Dumazet 				} else {
4530a14842fSEric Dumazet 					EMIT2(0x8b, 0x87);
4540a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, rxhash), 4);
4550a14842fSEric Dumazet 				}
4560a14842fSEric Dumazet 				break;
4570a14842fSEric Dumazet 			case BPF_S_ANC_QUEUE:
4580a14842fSEric Dumazet 				BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
4590a14842fSEric Dumazet 				if (is_imm8(offsetof(struct sk_buff, queue_mapping))) {
4600a14842fSEric Dumazet 					/* movzwl off8(%rdi),%eax */
4610a14842fSEric Dumazet 					EMIT4(0x0f, 0xb7, 0x47, offsetof(struct sk_buff, queue_mapping));
4620a14842fSEric Dumazet 				} else {
4630a14842fSEric Dumazet 					EMIT3(0x0f, 0xb7, 0x87); /* movzwl off32(%rdi),%eax */
4640a14842fSEric Dumazet 					EMIT(offsetof(struct sk_buff, queue_mapping), 4);
4650a14842fSEric Dumazet 				}
4660a14842fSEric Dumazet 				break;
4670a14842fSEric Dumazet 			case BPF_S_ANC_CPU:
4680a14842fSEric Dumazet #ifdef CONFIG_SMP
4690a14842fSEric Dumazet 				EMIT4(0x65, 0x8b, 0x04, 0x25); /* mov %gs:off32,%eax */
4700a14842fSEric Dumazet 				EMIT((u32)(unsigned long)&cpu_number, 4); /* A = smp_processor_id(); */
4710a14842fSEric Dumazet #else
4720a14842fSEric Dumazet 				CLEAR_A();
4730a14842fSEric Dumazet #endif
4740a14842fSEric Dumazet 				break;
4750a14842fSEric Dumazet 			case BPF_S_LD_W_ABS:
4760a14842fSEric Dumazet 				func = sk_load_word;
4770a14842fSEric Dumazet common_load:			seen |= SEEN_DATAREF;
4780a14842fSEric Dumazet 				if ((int)K < 0)
4790a14842fSEric Dumazet 					goto out;
4800a14842fSEric Dumazet 				t_offset = func - (image + addrs[i]);
4810a14842fSEric Dumazet 				EMIT1_off32(0xbe, K); /* mov imm32,%esi */
4820a14842fSEric Dumazet 				EMIT1_off32(0xe8, t_offset); /* call */
4830a14842fSEric Dumazet 				break;
4840a14842fSEric Dumazet 			case BPF_S_LD_H_ABS:
4850a14842fSEric Dumazet 				func = sk_load_half;
4860a14842fSEric Dumazet 				goto common_load;
4870a14842fSEric Dumazet 			case BPF_S_LD_B_ABS:
4880a14842fSEric Dumazet 				func = sk_load_byte;
4890a14842fSEric Dumazet 				goto common_load;
4900a14842fSEric Dumazet 			case BPF_S_LDX_B_MSH:
4910a14842fSEric Dumazet 				if ((int)K < 0) {
492d00a9dd2SEric Dumazet 					if (pc_ret0 > 0) {
493d00a9dd2SEric Dumazet 						/* addrs[pc_ret0 - 1] is the start address */
494d00a9dd2SEric Dumazet 						EMIT_JMP(addrs[pc_ret0 - 1] - addrs[i]);
4950a14842fSEric Dumazet 						break;
4960a14842fSEric Dumazet 					}
4970a14842fSEric Dumazet 					CLEAR_A();
4980a14842fSEric Dumazet 					EMIT_JMP(cleanup_addr - addrs[i]);
4990a14842fSEric Dumazet 					break;
5000a14842fSEric Dumazet 				}
5010a14842fSEric Dumazet 				seen |= SEEN_DATAREF | SEEN_XREG;
5020a14842fSEric Dumazet 				t_offset = sk_load_byte_msh - (image + addrs[i]);
5030a14842fSEric Dumazet 				EMIT1_off32(0xbe, K);	/* mov imm32,%esi */
5040a14842fSEric Dumazet 				EMIT1_off32(0xe8, t_offset); /* call sk_load_byte_msh */
5050a14842fSEric Dumazet 				break;
5060a14842fSEric Dumazet 			case BPF_S_LD_W_IND:
5070a14842fSEric Dumazet 				func = sk_load_word_ind;
5080a14842fSEric Dumazet common_load_ind:		seen |= SEEN_DATAREF | SEEN_XREG;
5090a14842fSEric Dumazet 				t_offset = func - (image + addrs[i]);
5100a14842fSEric Dumazet 				EMIT1_off32(0xbe, K);	/* mov imm32,%esi   */
5110a14842fSEric Dumazet 				EMIT1_off32(0xe8, t_offset);	/* call sk_load_xxx_ind */
5120a14842fSEric Dumazet 				break;
5130a14842fSEric Dumazet 			case BPF_S_LD_H_IND:
5140a14842fSEric Dumazet 				func = sk_load_half_ind;
5150a14842fSEric Dumazet 				goto common_load_ind;
5160a14842fSEric Dumazet 			case BPF_S_LD_B_IND:
5170a14842fSEric Dumazet 				func = sk_load_byte_ind;
5180a14842fSEric Dumazet 				goto common_load_ind;
5190a14842fSEric Dumazet 			case BPF_S_JMP_JA:
5200a14842fSEric Dumazet 				t_offset = addrs[i + K] - addrs[i];
5210a14842fSEric Dumazet 				EMIT_JMP(t_offset);
5220a14842fSEric Dumazet 				break;
5230a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JGT_K, X86_JA, X86_JBE);
5240a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JGE_K, X86_JAE, X86_JB);
5250a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JEQ_K, X86_JE, X86_JNE);
5260a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JSET_K,X86_JNE, X86_JE);
5270a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JGT_X, X86_JA, X86_JBE);
5280a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JGE_X, X86_JAE, X86_JB);
5290a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JEQ_X, X86_JE, X86_JNE);
5300a14842fSEric Dumazet 			COND_SEL(BPF_S_JMP_JSET_X,X86_JNE, X86_JE);
5310a14842fSEric Dumazet 
5320a14842fSEric Dumazet cond_branch:			f_offset = addrs[i + filter[i].jf] - addrs[i];
5330a14842fSEric Dumazet 				t_offset = addrs[i + filter[i].jt] - addrs[i];
5340a14842fSEric Dumazet 
5350a14842fSEric Dumazet 				/* same targets, can avoid doing the test :) */
5360a14842fSEric Dumazet 				if (filter[i].jt == filter[i].jf) {
5370a14842fSEric Dumazet 					EMIT_JMP(t_offset);
5380a14842fSEric Dumazet 					break;
5390a14842fSEric Dumazet 				}
5400a14842fSEric Dumazet 
5410a14842fSEric Dumazet 				switch (filter[i].code) {
5420a14842fSEric Dumazet 				case BPF_S_JMP_JGT_X:
5430a14842fSEric Dumazet 				case BPF_S_JMP_JGE_X:
5440a14842fSEric Dumazet 				case BPF_S_JMP_JEQ_X:
5450a14842fSEric Dumazet 					seen |= SEEN_XREG;
5460a14842fSEric Dumazet 					EMIT2(0x39, 0xd8); /* cmp %ebx,%eax */
5470a14842fSEric Dumazet 					break;
5480a14842fSEric Dumazet 				case BPF_S_JMP_JSET_X:
5490a14842fSEric Dumazet 					seen |= SEEN_XREG;
5500a14842fSEric Dumazet 					EMIT2(0x85, 0xd8); /* test %ebx,%eax */
5510a14842fSEric Dumazet 					break;
5520a14842fSEric Dumazet 				case BPF_S_JMP_JEQ_K:
5530a14842fSEric Dumazet 					if (K == 0) {
5540a14842fSEric Dumazet 						EMIT2(0x85, 0xc0); /* test   %eax,%eax */
5550a14842fSEric Dumazet 						break;
5560a14842fSEric Dumazet 					}
5570a14842fSEric Dumazet 				case BPF_S_JMP_JGT_K:
5580a14842fSEric Dumazet 				case BPF_S_JMP_JGE_K:
5590a14842fSEric Dumazet 					if (K <= 127)
5600a14842fSEric Dumazet 						EMIT3(0x83, 0xf8, K); /* cmp imm8,%eax */
5610a14842fSEric Dumazet 					else
5620a14842fSEric Dumazet 						EMIT1_off32(0x3d, K); /* cmp imm32,%eax */
5630a14842fSEric Dumazet 					break;
5640a14842fSEric Dumazet 				case BPF_S_JMP_JSET_K:
5650a14842fSEric Dumazet 					if (K <= 0xFF)
5660a14842fSEric Dumazet 						EMIT2(0xa8, K); /* test imm8,%al */
5670a14842fSEric Dumazet 					else if (!(K & 0xFFFF00FF))
5680a14842fSEric Dumazet 						EMIT3(0xf6, 0xc4, K >> 8); /* test imm8,%ah */
5690a14842fSEric Dumazet 					else if (K <= 0xFFFF) {
5700a14842fSEric Dumazet 						EMIT2(0x66, 0xa9); /* test imm16,%ax */
5710a14842fSEric Dumazet 						EMIT(K, 2);
5720a14842fSEric Dumazet 					} else {
5730a14842fSEric Dumazet 						EMIT1_off32(0xa9, K); /* test imm32,%eax */
5740a14842fSEric Dumazet 					}
5750a14842fSEric Dumazet 					break;
5760a14842fSEric Dumazet 				}
5770a14842fSEric Dumazet 				if (filter[i].jt != 0) {
578a03ffcf8SMarkus Kötter 					if (filter[i].jf && f_offset)
579a03ffcf8SMarkus Kötter 						t_offset += is_near(f_offset) ? 2 : 5;
5800a14842fSEric Dumazet 					EMIT_COND_JMP(t_op, t_offset);
5810a14842fSEric Dumazet 					if (filter[i].jf)
5820a14842fSEric Dumazet 						EMIT_JMP(f_offset);
5830a14842fSEric Dumazet 					break;
5840a14842fSEric Dumazet 				}
5850a14842fSEric Dumazet 				EMIT_COND_JMP(f_op, f_offset);
5860a14842fSEric Dumazet 				break;
5870a14842fSEric Dumazet 			default:
5880a14842fSEric Dumazet 				/* hmm, too complex filter, give up with jit compiler */
5890a14842fSEric Dumazet 				goto out;
5900a14842fSEric Dumazet 			}
5910a14842fSEric Dumazet 			ilen = prog - temp;
5920a14842fSEric Dumazet 			if (image) {
5930a14842fSEric Dumazet 				if (unlikely(proglen + ilen > oldproglen)) {
5940a14842fSEric Dumazet 					pr_err("bpb_jit_compile fatal error\n");
5950a14842fSEric Dumazet 					kfree(addrs);
5960a14842fSEric Dumazet 					module_free(NULL, image);
5970a14842fSEric Dumazet 					return;
5980a14842fSEric Dumazet 				}
5990a14842fSEric Dumazet 				memcpy(image + proglen, temp, ilen);
6000a14842fSEric Dumazet 			}
6010a14842fSEric Dumazet 			proglen += ilen;
6020a14842fSEric Dumazet 			addrs[i] = proglen;
6030a14842fSEric Dumazet 			prog = temp;
6040a14842fSEric Dumazet 		}
6050a14842fSEric Dumazet 		/* last bpf instruction is always a RET :
6060a14842fSEric Dumazet 		 * use it to give the cleanup instruction(s) addr
6070a14842fSEric Dumazet 		 */
6080a14842fSEric Dumazet 		cleanup_addr = proglen - 1; /* ret */
609d00a9dd2SEric Dumazet 		if (seen_or_pass0)
6100a14842fSEric Dumazet 			cleanup_addr -= 1; /* leaveq */
611d00a9dd2SEric Dumazet 		if (seen_or_pass0 & SEEN_XREG)
6120a14842fSEric Dumazet 			cleanup_addr -= 4; /* mov  -8(%rbp),%rbx */
6130a14842fSEric Dumazet 
6140a14842fSEric Dumazet 		if (image) {
615d00a9dd2SEric Dumazet 			if (proglen != oldproglen)
616d00a9dd2SEric Dumazet 				pr_err("bpb_jit_compile proglen=%u != oldproglen=%u\n", proglen, oldproglen);
6170a14842fSEric Dumazet 			break;
6180a14842fSEric Dumazet 		}
6190a14842fSEric Dumazet 		if (proglen == oldproglen) {
6200a14842fSEric Dumazet 			image = module_alloc(max_t(unsigned int,
6210a14842fSEric Dumazet 						   proglen,
6220a14842fSEric Dumazet 						   sizeof(struct work_struct)));
6230a14842fSEric Dumazet 			if (!image)
6240a14842fSEric Dumazet 				goto out;
6250a14842fSEric Dumazet 		}
6260a14842fSEric Dumazet 		oldproglen = proglen;
6270a14842fSEric Dumazet 	}
6280a14842fSEric Dumazet 	if (bpf_jit_enable > 1)
6290a14842fSEric Dumazet 		pr_err("flen=%d proglen=%u pass=%d image=%p\n",
6300a14842fSEric Dumazet 		       flen, proglen, pass, image);
6310a14842fSEric Dumazet 
6320a14842fSEric Dumazet 	if (image) {
6330a14842fSEric Dumazet 		if (bpf_jit_enable > 1)
6340a14842fSEric Dumazet 			print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_ADDRESS,
6350a14842fSEric Dumazet 				       16, 1, image, proglen, false);
6360a14842fSEric Dumazet 
6370a14842fSEric Dumazet 		bpf_flush_icache(image, image + proglen);
6380a14842fSEric Dumazet 
6390a14842fSEric Dumazet 		fp->bpf_func = (void *)image;
6400a14842fSEric Dumazet 	}
6410a14842fSEric Dumazet out:
6420a14842fSEric Dumazet 	kfree(addrs);
6430a14842fSEric Dumazet 	return;
6440a14842fSEric Dumazet }
6450a14842fSEric Dumazet 
6460a14842fSEric Dumazet static void jit_free_defer(struct work_struct *arg)
6470a14842fSEric Dumazet {
6480a14842fSEric Dumazet 	module_free(NULL, arg);
6490a14842fSEric Dumazet }
6500a14842fSEric Dumazet 
6510a14842fSEric Dumazet /* run from softirq, we must use a work_struct to call
6520a14842fSEric Dumazet  * module_free() from process context
6530a14842fSEric Dumazet  */
6540a14842fSEric Dumazet void bpf_jit_free(struct sk_filter *fp)
6550a14842fSEric Dumazet {
6560a14842fSEric Dumazet 	if (fp->bpf_func != sk_run_filter) {
6570a14842fSEric Dumazet 		struct work_struct *work = (struct work_struct *)fp->bpf_func;
6580a14842fSEric Dumazet 
6590a14842fSEric Dumazet 		INIT_WORK(work, jit_free_defer);
6600a14842fSEric Dumazet 		schedule_work(work);
6610a14842fSEric Dumazet 	}
6620a14842fSEric Dumazet }
663