1 /* bpf_jit_comp.c : BPF JIT compiler
3 * Copyright (C) 2011 Eric Dumazet (eric.dumazet@gmail.com)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; version 2
10 #include <linux/moduleloader.h>
11 #include <asm/cacheflush.h>
12 #include <linux/netdevice.h>
13 #include <linux/filter.h>
14 #include <linux/if_vlan.h>
18 * EAX : BPF A accumulator
19 * EBX : BPF X accumulator
20 * RDI : pointer to skb (first argument given to JIT function)
21 * RBP : frame pointer (even if CONFIG_FRAME_POINTER=n)
22 * ECX,EDX,ESI : scratch registers
23 * r9d : skb->len - skb->data_len (headlen)
25 * -8(RBP) : saved RBX value
26 * -16(RBP)..-80(RBP) : BPF_MEMWORDS values
28 int bpf_jit_enable __read_mostly
;
31 * assembly code in arch/x86/net/bpf_jit.S
33 extern u8 sk_load_word
[], sk_load_half
[], sk_load_byte
[], sk_load_byte_msh
[];
34 extern u8 sk_load_word_positive_offset
[], sk_load_half_positive_offset
[];
35 extern u8 sk_load_byte_positive_offset
[], sk_load_byte_msh_positive_offset
[];
36 extern u8 sk_load_word_negative_offset
[], sk_load_half_negative_offset
[];
37 extern u8 sk_load_byte_negative_offset
[], sk_load_byte_msh_negative_offset
[];
39 static inline u8
*emit_code(u8
*ptr
, u32 bytes
, unsigned int len
)
52 #define EMIT(bytes, len) do { prog = emit_code(prog, bytes, len); } while (0)
54 #define EMIT1(b1) EMIT(b1, 1)
55 #define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
56 #define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
57 #define EMIT4(b1, b2, b3, b4) EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
58 #define EMIT1_off32(b1, off) do { EMIT1(b1); EMIT(off, 4);} while (0)
60 #define CLEAR_A() EMIT2(0x31, 0xc0) /* xor %eax,%eax */
61 #define CLEAR_X() EMIT2(0x31, 0xdb) /* xor %ebx,%ebx */
63 static inline bool is_imm8(int value
)
65 return value
<= 127 && value
>= -128;
68 static inline bool is_near(int offset
)
70 return offset
<= 127 && offset
>= -128;
73 #define EMIT_JMP(offset) \
76 if (is_near(offset)) \
77 EMIT2(0xeb, offset); /* jmp .+off8 */ \
79 EMIT1_off32(0xe9, offset); /* jmp .+off32 */ \
83 /* list of x86 cond jumps opcodes (. + s8)
84 * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
93 #define EMIT_COND_JMP(op, offset) \
95 if (is_near(offset)) \
96 EMIT2(op, offset); /* jxx .+off8 */ \
98 EMIT2(0x0f, op + 0x10); \
99 EMIT(offset, 4); /* jxx .+off32 */ \
103 #define COND_SEL(CODE, TOP, FOP) \
110 #define SEEN_DATAREF 1 /* might call external helpers */
111 #define SEEN_XREG 2 /* ebx is used */
112 #define SEEN_MEM 4 /* use mem[] for temporary storage */
114 static inline void bpf_flush_icache(void *start
, void *end
)
116 mm_segment_t old_fs
= get_fs();
120 flush_icache_range((unsigned long)start
, (unsigned long)end
);
124 #define CHOOSE_LOAD_FUNC(K, func) \
125 ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
127 void bpf_jit_compile(struct sk_filter
*fp
)
131 unsigned int proglen
, oldproglen
= 0;
133 int t_offset
, f_offset
;
134 u8 t_op
, f_op
, seen
= 0, pass
;
137 int pc_ret0
= -1; /* bpf index of first RET #0 instruction (if any) */
138 unsigned int cleanup_addr
; /* epilogue code offset */
140 const struct sock_filter
*filter
= fp
->insns
;
146 addrs
= kmalloc(flen
* sizeof(*addrs
), GFP_KERNEL
);
150 /* Before first pass, make a rough estimation of addrs[]
151 * each bpf instruction is translated to less than 64 bytes
153 for (proglen
= 0, i
= 0; i
< flen
; i
++) {
157 cleanup_addr
= proglen
; /* epilogue address */
159 for (pass
= 0; pass
< 10; pass
++) {
160 u8 seen_or_pass0
= (pass
== 0) ? (SEEN_XREG
| SEEN_DATAREF
| SEEN_MEM
) : seen
;
161 /* no prologue/epilogue for trivial filters (RET something) */
166 EMIT4(0x55, 0x48, 0x89, 0xe5); /* push %rbp; mov %rsp,%rbp */
167 EMIT4(0x48, 0x83, 0xec, 96); /* subq $96,%rsp */
168 /* note : must save %rbx in case bpf_error is hit */
169 if (seen_or_pass0
& (SEEN_XREG
| SEEN_DATAREF
))
170 EMIT4(0x48, 0x89, 0x5d, 0xf8); /* mov %rbx, -8(%rbp) */
171 if (seen_or_pass0
& SEEN_XREG
)
172 CLEAR_X(); /* make sure we dont leek kernel memory */
175 * If this filter needs to access skb data,
176 * loads r9 and r8 with :
177 * r9 = skb->len - skb->data_len
180 if (seen_or_pass0
& SEEN_DATAREF
) {
181 if (offsetof(struct sk_buff
, len
) <= 127)
182 /* mov off8(%rdi),%r9d */
183 EMIT4(0x44, 0x8b, 0x4f, offsetof(struct sk_buff
, len
));
185 /* mov off32(%rdi),%r9d */
186 EMIT3(0x44, 0x8b, 0x8f);
187 EMIT(offsetof(struct sk_buff
, len
), 4);
189 if (is_imm8(offsetof(struct sk_buff
, data_len
)))
190 /* sub off8(%rdi),%r9d */
191 EMIT4(0x44, 0x2b, 0x4f, offsetof(struct sk_buff
, data_len
));
193 EMIT3(0x44, 0x2b, 0x8f);
194 EMIT(offsetof(struct sk_buff
, data_len
), 4);
197 if (is_imm8(offsetof(struct sk_buff
, data
)))
198 /* mov off8(%rdi),%r8 */
199 EMIT4(0x4c, 0x8b, 0x47, offsetof(struct sk_buff
, data
));
201 /* mov off32(%rdi),%r8 */
202 EMIT3(0x4c, 0x8b, 0x87);
203 EMIT(offsetof(struct sk_buff
, data
), 4);
208 switch (filter
[0].code
) {
211 case BPF_S_ANC_PROTOCOL
:
212 case BPF_S_ANC_IFINDEX
:
214 case BPF_S_ANC_RXHASH
:
216 case BPF_S_ANC_VLAN_TAG
:
217 case BPF_S_ANC_VLAN_TAG_PRESENT
:
218 case BPF_S_ANC_QUEUE
:
222 /* first instruction sets A register (or is RET 'constant') */
225 /* make sure we dont leak kernel information to user */
226 CLEAR_A(); /* A = 0 */
229 for (i
= 0; i
< flen
; i
++) {
230 unsigned int K
= filter
[i
].k
;
232 switch (filter
[i
].code
) {
233 case BPF_S_ALU_ADD_X
: /* A += X; */
235 EMIT2(0x01, 0xd8); /* add %ebx,%eax */
237 case BPF_S_ALU_ADD_K
: /* A += K; */
241 EMIT3(0x83, 0xc0, K
); /* add imm8,%eax */
243 EMIT1_off32(0x05, K
); /* add imm32,%eax */
245 case BPF_S_ALU_SUB_X
: /* A -= X; */
247 EMIT2(0x29, 0xd8); /* sub %ebx,%eax */
249 case BPF_S_ALU_SUB_K
: /* A -= K */
253 EMIT3(0x83, 0xe8, K
); /* sub imm8,%eax */
255 EMIT1_off32(0x2d, K
); /* sub imm32,%eax */
257 case BPF_S_ALU_MUL_X
: /* A *= X; */
259 EMIT3(0x0f, 0xaf, 0xc3); /* imul %ebx,%eax */
261 case BPF_S_ALU_MUL_K
: /* A *= K */
263 EMIT3(0x6b, 0xc0, K
); /* imul imm8,%eax,%eax */
265 EMIT2(0x69, 0xc0); /* imul imm32,%eax */
269 case BPF_S_ALU_DIV_X
: /* A /= X; */
271 EMIT2(0x85, 0xdb); /* test %ebx,%ebx */
273 /* addrs[pc_ret0 - 1] is start address of target
274 * (addrs[i] - 4) is the address following this jmp
275 * ("xor %edx,%edx; div %ebx" being 4 bytes long)
277 EMIT_COND_JMP(X86_JE
, addrs
[pc_ret0
- 1] -
280 EMIT_COND_JMP(X86_JNE
, 2 + 5);
282 EMIT1_off32(0xe9, cleanup_addr
- (addrs
[i
] - 4)); /* jmp .+off32 */
284 EMIT4(0x31, 0xd2, 0xf7, 0xf3); /* xor %edx,%edx; div %ebx */
286 case BPF_S_ALU_MOD_X
: /* A %= X; */
288 EMIT2(0x85, 0xdb); /* test %ebx,%ebx */
290 /* addrs[pc_ret0 - 1] is start address of target
291 * (addrs[i] - 6) is the address following this jmp
292 * ("xor %edx,%edx; div %ebx;mov %edx,%eax" being 6 bytes long)
294 EMIT_COND_JMP(X86_JE
, addrs
[pc_ret0
- 1] -
297 EMIT_COND_JMP(X86_JNE
, 2 + 5);
299 EMIT1_off32(0xe9, cleanup_addr
- (addrs
[i
] - 6)); /* jmp .+off32 */
301 EMIT2(0x31, 0xd2); /* xor %edx,%edx */
302 EMIT2(0xf7, 0xf3); /* div %ebx */
303 EMIT2(0x89, 0xd0); /* mov %edx,%eax */
305 case BPF_S_ALU_MOD_K
: /* A %= K; */
306 EMIT2(0x31, 0xd2); /* xor %edx,%edx */
307 EMIT1(0xb9);EMIT(K
, 4); /* mov imm32,%ecx */
308 EMIT2(0xf7, 0xf1); /* div %ecx */
309 EMIT2(0x89, 0xd0); /* mov %edx,%eax */
311 case BPF_S_ALU_DIV_K
: /* A = reciprocal_divide(A, K); */
312 EMIT3(0x48, 0x69, 0xc0); /* imul imm32,%rax,%rax */
314 EMIT4(0x48, 0xc1, 0xe8, 0x20); /* shr $0x20,%rax */
316 case BPF_S_ALU_AND_X
:
318 EMIT2(0x21, 0xd8); /* and %ebx,%eax */
320 case BPF_S_ALU_AND_K
:
321 if (K
>= 0xFFFFFF00) {
322 EMIT2(0x24, K
& 0xFF); /* and imm8,%al */
323 } else if (K
>= 0xFFFF0000) {
324 EMIT2(0x66, 0x25); /* and imm16,%ax */
327 EMIT1_off32(0x25, K
); /* and imm32,%eax */
332 EMIT2(0x09, 0xd8); /* or %ebx,%eax */
336 EMIT3(0x83, 0xc8, K
); /* or imm8,%eax */
338 EMIT1_off32(0x0d, K
); /* or imm32,%eax */
340 case BPF_S_ANC_ALU_XOR_X
: /* A ^= X; */
341 case BPF_S_ALU_XOR_X
:
343 EMIT2(0x31, 0xd8); /* xor %ebx,%eax */
345 case BPF_S_ALU_XOR_K
: /* A ^= K; */
349 EMIT3(0x83, 0xf0, K
); /* xor imm8,%eax */
351 EMIT1_off32(0x35, K
); /* xor imm32,%eax */
353 case BPF_S_ALU_LSH_X
: /* A <<= X; */
355 EMIT4(0x89, 0xd9, 0xd3, 0xe0); /* mov %ebx,%ecx; shl %cl,%eax */
357 case BPF_S_ALU_LSH_K
:
361 EMIT2(0xd1, 0xe0); /* shl %eax */
363 EMIT3(0xc1, 0xe0, K
);
365 case BPF_S_ALU_RSH_X
: /* A >>= X; */
367 EMIT4(0x89, 0xd9, 0xd3, 0xe8); /* mov %ebx,%ecx; shr %cl,%eax */
369 case BPF_S_ALU_RSH_K
: /* A >>= K; */
373 EMIT2(0xd1, 0xe8); /* shr %eax */
375 EMIT3(0xc1, 0xe8, K
);
378 EMIT2(0xf7, 0xd8); /* neg %eax */
386 EMIT1_off32(0xb8, K
); /* mov $imm32,%eax */
392 EMIT_JMP(cleanup_addr
- addrs
[i
]);
395 if (seen_or_pass0
& SEEN_XREG
)
396 EMIT4(0x48, 0x8b, 0x5d, 0xf8); /* mov -8(%rbp),%rbx */
397 EMIT1(0xc9); /* leaveq */
399 EMIT1(0xc3); /* ret */
401 case BPF_S_MISC_TAX
: /* X = A */
403 EMIT2(0x89, 0xc3); /* mov %eax,%ebx */
405 case BPF_S_MISC_TXA
: /* A = X */
407 EMIT2(0x89, 0xd8); /* mov %ebx,%eax */
409 case BPF_S_LD_IMM
: /* A = K */
413 EMIT1_off32(0xb8, K
); /* mov $imm32,%eax */
415 case BPF_S_LDX_IMM
: /* X = K */
420 EMIT1_off32(0xbb, K
); /* mov $imm32,%ebx */
422 case BPF_S_LD_MEM
: /* A = mem[K] : mov off8(%rbp),%eax */
424 EMIT3(0x8b, 0x45, 0xf0 - K
*4);
426 case BPF_S_LDX_MEM
: /* X = mem[K] : mov off8(%rbp),%ebx */
427 seen
|= SEEN_XREG
| SEEN_MEM
;
428 EMIT3(0x8b, 0x5d, 0xf0 - K
*4);
430 case BPF_S_ST
: /* mem[K] = A : mov %eax,off8(%rbp) */
432 EMIT3(0x89, 0x45, 0xf0 - K
*4);
434 case BPF_S_STX
: /* mem[K] = X : mov %ebx,off8(%rbp) */
435 seen
|= SEEN_XREG
| SEEN_MEM
;
436 EMIT3(0x89, 0x5d, 0xf0 - K
*4);
438 case BPF_S_LD_W_LEN
: /* A = skb->len; */
439 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, len
) != 4);
440 if (is_imm8(offsetof(struct sk_buff
, len
)))
441 /* mov off8(%rdi),%eax */
442 EMIT3(0x8b, 0x47, offsetof(struct sk_buff
, len
));
445 EMIT(offsetof(struct sk_buff
, len
), 4);
448 case BPF_S_LDX_W_LEN
: /* X = skb->len; */
450 if (is_imm8(offsetof(struct sk_buff
, len
)))
451 /* mov off8(%rdi),%ebx */
452 EMIT3(0x8b, 0x5f, offsetof(struct sk_buff
, len
));
455 EMIT(offsetof(struct sk_buff
, len
), 4);
458 case BPF_S_ANC_PROTOCOL
: /* A = ntohs(skb->protocol); */
459 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, protocol
) != 2);
460 if (is_imm8(offsetof(struct sk_buff
, protocol
))) {
461 /* movzwl off8(%rdi),%eax */
462 EMIT4(0x0f, 0xb7, 0x47, offsetof(struct sk_buff
, protocol
));
464 EMIT3(0x0f, 0xb7, 0x87); /* movzwl off32(%rdi),%eax */
465 EMIT(offsetof(struct sk_buff
, protocol
), 4);
467 EMIT2(0x86, 0xc4); /* ntohs() : xchg %al,%ah */
469 case BPF_S_ANC_IFINDEX
:
470 if (is_imm8(offsetof(struct sk_buff
, dev
))) {
471 /* movq off8(%rdi),%rax */
472 EMIT4(0x48, 0x8b, 0x47, offsetof(struct sk_buff
, dev
));
474 EMIT3(0x48, 0x8b, 0x87); /* movq off32(%rdi),%rax */
475 EMIT(offsetof(struct sk_buff
, dev
), 4);
477 EMIT3(0x48, 0x85, 0xc0); /* test %rax,%rax */
478 EMIT_COND_JMP(X86_JE
, cleanup_addr
- (addrs
[i
] - 6));
479 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device
, ifindex
) != 4);
480 EMIT2(0x8b, 0x80); /* mov off32(%rax),%eax */
481 EMIT(offsetof(struct net_device
, ifindex
), 4);
484 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, mark
) != 4);
485 if (is_imm8(offsetof(struct sk_buff
, mark
))) {
486 /* mov off8(%rdi),%eax */
487 EMIT3(0x8b, 0x47, offsetof(struct sk_buff
, mark
));
490 EMIT(offsetof(struct sk_buff
, mark
), 4);
493 case BPF_S_ANC_RXHASH
:
494 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, rxhash
) != 4);
495 if (is_imm8(offsetof(struct sk_buff
, rxhash
))) {
496 /* mov off8(%rdi),%eax */
497 EMIT3(0x8b, 0x47, offsetof(struct sk_buff
, rxhash
));
500 EMIT(offsetof(struct sk_buff
, rxhash
), 4);
503 case BPF_S_ANC_QUEUE
:
504 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, queue_mapping
) != 2);
505 if (is_imm8(offsetof(struct sk_buff
, queue_mapping
))) {
506 /* movzwl off8(%rdi),%eax */
507 EMIT4(0x0f, 0xb7, 0x47, offsetof(struct sk_buff
, queue_mapping
));
509 EMIT3(0x0f, 0xb7, 0x87); /* movzwl off32(%rdi),%eax */
510 EMIT(offsetof(struct sk_buff
, queue_mapping
), 4);
515 EMIT4(0x65, 0x8b, 0x04, 0x25); /* mov %gs:off32,%eax */
516 EMIT((u32
)(unsigned long)&cpu_number
, 4); /* A = smp_processor_id(); */
521 case BPF_S_ANC_VLAN_TAG
:
522 case BPF_S_ANC_VLAN_TAG_PRESENT
:
523 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, vlan_tci
) != 2);
524 if (is_imm8(offsetof(struct sk_buff
, vlan_tci
))) {
525 /* movzwl off8(%rdi),%eax */
526 EMIT4(0x0f, 0xb7, 0x47, offsetof(struct sk_buff
, vlan_tci
));
528 EMIT3(0x0f, 0xb7, 0x87); /* movzwl off32(%rdi),%eax */
529 EMIT(offsetof(struct sk_buff
, vlan_tci
), 4);
531 BUILD_BUG_ON(VLAN_TAG_PRESENT
!= 0x1000);
532 if (filter
[i
].code
== BPF_S_ANC_VLAN_TAG
) {
533 EMIT3(0x80, 0xe4, 0xef); /* and $0xef,%ah */
535 EMIT3(0xc1, 0xe8, 0x0c); /* shr $0xc,%eax */
536 EMIT3(0x83, 0xe0, 0x01); /* and $0x1,%eax */
540 func
= CHOOSE_LOAD_FUNC(K
, sk_load_word
);
541 common_load
: seen
|= SEEN_DATAREF
;
542 t_offset
= func
- (image
+ addrs
[i
]);
543 EMIT1_off32(0xbe, K
); /* mov imm32,%esi */
544 EMIT1_off32(0xe8, t_offset
); /* call */
547 func
= CHOOSE_LOAD_FUNC(K
, sk_load_half
);
550 func
= CHOOSE_LOAD_FUNC(K
, sk_load_byte
);
552 case BPF_S_LDX_B_MSH
:
553 func
= CHOOSE_LOAD_FUNC(K
, sk_load_byte_msh
);
554 seen
|= SEEN_DATAREF
| SEEN_XREG
;
555 t_offset
= func
- (image
+ addrs
[i
]);
556 EMIT1_off32(0xbe, K
); /* mov imm32,%esi */
557 EMIT1_off32(0xe8, t_offset
); /* call sk_load_byte_msh */
561 common_load_ind
: seen
|= SEEN_DATAREF
| SEEN_XREG
;
562 t_offset
= func
- (image
+ addrs
[i
]);
565 EMIT3(0x8d, 0x73, K
); /* lea imm8(%rbx), %esi */
567 EMIT2(0x8d, 0xb3); /* lea imm32(%rbx),%esi */
571 EMIT2(0x89,0xde); /* mov %ebx,%esi */
573 EMIT1_off32(0xe8, t_offset
); /* call sk_load_xxx_ind */
577 goto common_load_ind
;
580 goto common_load_ind
;
582 t_offset
= addrs
[i
+ K
] - addrs
[i
];
585 COND_SEL(BPF_S_JMP_JGT_K
, X86_JA
, X86_JBE
);
586 COND_SEL(BPF_S_JMP_JGE_K
, X86_JAE
, X86_JB
);
587 COND_SEL(BPF_S_JMP_JEQ_K
, X86_JE
, X86_JNE
);
588 COND_SEL(BPF_S_JMP_JSET_K
,X86_JNE
, X86_JE
);
589 COND_SEL(BPF_S_JMP_JGT_X
, X86_JA
, X86_JBE
);
590 COND_SEL(BPF_S_JMP_JGE_X
, X86_JAE
, X86_JB
);
591 COND_SEL(BPF_S_JMP_JEQ_X
, X86_JE
, X86_JNE
);
592 COND_SEL(BPF_S_JMP_JSET_X
,X86_JNE
, X86_JE
);
594 cond_branch
: f_offset
= addrs
[i
+ filter
[i
].jf
] - addrs
[i
];
595 t_offset
= addrs
[i
+ filter
[i
].jt
] - addrs
[i
];
597 /* same targets, can avoid doing the test :) */
598 if (filter
[i
].jt
== filter
[i
].jf
) {
603 switch (filter
[i
].code
) {
604 case BPF_S_JMP_JGT_X
:
605 case BPF_S_JMP_JGE_X
:
606 case BPF_S_JMP_JEQ_X
:
608 EMIT2(0x39, 0xd8); /* cmp %ebx,%eax */
610 case BPF_S_JMP_JSET_X
:
612 EMIT2(0x85, 0xd8); /* test %ebx,%eax */
614 case BPF_S_JMP_JEQ_K
:
616 EMIT2(0x85, 0xc0); /* test %eax,%eax */
619 case BPF_S_JMP_JGT_K
:
620 case BPF_S_JMP_JGE_K
:
622 EMIT3(0x83, 0xf8, K
); /* cmp imm8,%eax */
624 EMIT1_off32(0x3d, K
); /* cmp imm32,%eax */
626 case BPF_S_JMP_JSET_K
:
628 EMIT2(0xa8, K
); /* test imm8,%al */
629 else if (!(K
& 0xFFFF00FF))
630 EMIT3(0xf6, 0xc4, K
>> 8); /* test imm8,%ah */
631 else if (K
<= 0xFFFF) {
632 EMIT2(0x66, 0xa9); /* test imm16,%ax */
635 EMIT1_off32(0xa9, K
); /* test imm32,%eax */
639 if (filter
[i
].jt
!= 0) {
640 if (filter
[i
].jf
&& f_offset
)
641 t_offset
+= is_near(f_offset
) ? 2 : 5;
642 EMIT_COND_JMP(t_op
, t_offset
);
647 EMIT_COND_JMP(f_op
, f_offset
);
650 /* hmm, too complex filter, give up with jit compiler */
655 if (unlikely(proglen
+ ilen
> oldproglen
)) {
656 pr_err("bpb_jit_compile fatal error\n");
658 module_free(NULL
, image
);
661 memcpy(image
+ proglen
, temp
, ilen
);
667 /* last bpf instruction is always a RET :
668 * use it to give the cleanup instruction(s) addr
670 cleanup_addr
= proglen
- 1; /* ret */
672 cleanup_addr
-= 1; /* leaveq */
673 if (seen_or_pass0
& SEEN_XREG
)
674 cleanup_addr
-= 4; /* mov -8(%rbp),%rbx */
677 if (proglen
!= oldproglen
)
678 pr_err("bpb_jit_compile proglen=%u != oldproglen=%u\n", proglen
, oldproglen
);
681 if (proglen
== oldproglen
) {
682 image
= module_alloc(max_t(unsigned int,
684 sizeof(struct work_struct
)));
688 oldproglen
= proglen
;
690 if (bpf_jit_enable
> 1)
691 pr_err("flen=%d proglen=%u pass=%d image=%p\n",
692 flen
, proglen
, pass
, image
);
695 if (bpf_jit_enable
> 1)
696 print_hex_dump(KERN_ERR
, "JIT code: ", DUMP_PREFIX_ADDRESS
,
697 16, 1, image
, proglen
, false);
699 bpf_flush_icache(image
, image
+ proglen
);
701 fp
->bpf_func
= (void *)image
;
708 static void jit_free_defer(struct work_struct
*arg
)
710 module_free(NULL
, arg
);
713 /* run from softirq, we must use a work_struct to call
714 * module_free() from process context
716 void bpf_jit_free(struct sk_filter
*fp
)
718 if (fp
->bpf_func
!= sk_run_filter
) {
719 struct work_struct
*work
= (struct work_struct
*)fp
->bpf_func
;
721 INIT_WORK(work
, jit_free_defer
);