Skip to contentSkip to conversation

bpf, x86_64: fix JIT to dump the valid insn

bpf-next [email protected] 2 participants 3 messages Dec 30, 2022 – Jan 3, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

From: Tonghao Zhang <[email protected]>

With bpf_jit_binary_pack_finalize invoked, we copy rw_header to header and then image/insn is valid.

Write a test BPF prog which include subprog:

$ llvm-objdump -d subprog.o
Disassembly of section .text:
0000000000000000 <subprog>:
       0:	18 01 00 00 73 75 62 70 00 00 00 00 72 6f 67 00	r1 = 29114459903653235 ll
       2:	7b 1a f8 ff 00 00 00 00	*(u64 *)(r10 - 8) = r1
       3:	bf a1 00 00 00 00 00 00	r1 = r10
       4:	07 01 00 00 f8 ff ff ff	r1 += -8
       5:	b7 02 00 00 08 00 00 00	r2 = 8
       6:	85 00 00 00 06 00 00 00	call 6
       7:	95 00 00 00 00 00 00 00	exit
Disassembly of section raw_tp/sys_enter:
0000000000000000 <entry>:
       0:	85 10 00 00 ff ff ff ff	call -1
       1:	b7 00 00 00 00 00 00 00	r0 = 0
       2:	95 00 00 00 00 00 00 00	exit

Without this patch, kernel print message:

[  580.775387] flen=8 proglen=51 pass=3 image=ffffffffa000c20c from=kprobe-load pid=1643
[  580.777236] JIT code: 00000000: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
[  580.779037] JIT code: 00000010: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
[  580.780767] JIT code: 00000020: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
[  580.782568] JIT code: 00000030: cc cc cc
$ bpf_jit_disasm
51 bytes emitted from JIT compiler (pass:3, flen:8)
ffffffffa000c20c + <x>:
   0:	int3
   1:	int3
   2:	int3
   3:	int3
   4:	int3
   5:	int3
   ...

To fix this issue:

[  260.016071] flen=3 proglen=20 pass=1 image=ffffffffa000c11c from=kprobe-load pid=1568
[  260.018094] JIT code: 00000000: 0f 1f 44 00 00 66 90 55 48 89 e5 e8 38 00 00 00
[  260.020124] JIT code: 00000010: 31 c0 c9 c3
[  260.021229] flen=8 proglen=51 pass=1 image=ffffffffa000c164 from=kprobe-load pid=1568
[  260.023132] JIT code: 00000000: 0f 1f 44 00 00 66 90 55 48 89 e5 48 81 ec 08 00
[  260.025129] JIT code: 00000010: 00 00 48 bf 73 75 62 70 72 6f 67 00 48 89 7d f8
[  260.027199] JIT code: 00000020: 48 89 ef 48 83 c7 f8 be 08 00 00 00 e8 9e 19 1d
[  260.029226] JIT code: 00000030: e1 c9 c3
$ bpf_jit_disasm
51 bytes emitted from JIT compiler (pass:1, flen:8)
ffffffffa000c164 + <x>:
   0:	nopl   0x0(%rax,%rax,1)
   5:	xchg   %ax,%ax
   7:	push   %rbp
   8:	mov    %rsp,%rbp
   b:	sub    $0x8,%rsp
  12:	movabs $0x676f7270627573,%rdi
  1c:	mov    %rdi,-0x8(%rbp)
  20:	mov    %rbp,%rdi
  23:	add    $0xfffffffffffffff8,%rdi
  27:	mov    $0x8,%esi
  2c:	callq  0xffffffffe11d19cf
  31:	leaveq
  32:	retq
$ bpf_jit_disasm
20 bytes emitted from JIT compiler (pass:1, flen:3)
ffffffffa000c11c + <x>:
   0:	nopl   0x0(%rax,%rax,1)
   5:	xchg   %ax,%ax
   7:	push   %rbp
   8:	mov    %rsp,%rbp
   b:	callq  0x0000000000000048
  10:	xor    %eax,%eax
  12:	leaveq
  13:	retq
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc John Fastabend <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Hou Tao <[email protected]>
---
 arch/x86/net/bpf_jit_comp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/net/bpf_jit_comp.c +3 -3
index 36ffe67ad6e5..4e017102cc16 100644
@@ -2543,9 +2543,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
 		cond_resched();
 	}
 
-	if (bpf_jit_enable > 1)
-		bpf_jit_dump(prog->len, proglen, pass + 1, image);
-
 	if (image) {
 		if (!prog->is_func || extra_pass) {
 			/*
@@ -2561,6 +2558,9 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
 				goto out_image;
 			}
 
+			if (bpf_jit_enable > 1)
+				bpf_jit_dump(prog->len, proglen, pass + 1, image);
+
 			bpf_tail_call_direct_fixup(prog);
 		} else {
 			jit_data->addrs = addrs;
Signature · 1 line
2.27.0
#1 lore raw
Quoted 113 lines · On Fri, Dec 30, 2022 at 3:38 AM <[email protected]> wrote:

From: Tonghao Zhang <[email protected]>

With bpf_jit_binary_pack_finalize invoked, we copy rw_header to header and then image/insn is valid.

Write a test BPF prog which include subprog:

$ llvm-objdump -d subprog.o
Disassembly of section .text:
0000000000000000 <subprog>:
       0:       18 01 00 00 73 75 62 70 00 00 00 00 72 6f 67 00 r1 = 29114459903653235 ll
       2:       7b 1a f8 ff 00 00 00 00 *(u64 *)(r10 - 8) = r1
       3:       bf a1 00 00 00 00 00 00 r1 = r10
       4:       07 01 00 00 f8 ff ff ff r1 += -8
       5:       b7 02 00 00 08 00 00 00 r2 = 8
       6:       85 00 00 00 06 00 00 00 call 6
       7:       95 00 00 00 00 00 00 00 exit
Disassembly of section raw_tp/sys_enter:
0000000000000000 <entry>:
       0:       85 10 00 00 ff ff ff ff call -1
       1:       b7 00 00 00 00 00 00 00 r0 = 0
       2:       95 00 00 00 00 00 00 00 exit

Without this patch, kernel print message:

[  580.775387] flen=8 proglen=51 pass=3 image=ffffffffa000c20c from=kprobe-load pid=1643
[  580.777236] JIT code: 00000000: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
[  580.779037] JIT code: 00000010: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
[  580.780767] JIT code: 00000020: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
[  580.782568] JIT code: 00000030: cc cc cc
$ bpf_jit_disasm
51 bytes emitted from JIT compiler (pass:3, flen:8)
ffffffffa000c20c + <x>:
   0:   int3
   1:   int3
   2:   int3
   3:   int3
   4:   int3
   5:   int3
   ...

To fix this issue:

[  260.016071] flen=3 proglen=20 pass=1 image=ffffffffa000c11c from=kprobe-load pid=1568
[  260.018094] JIT code: 00000000: 0f 1f 44 00 00 66 90 55 48 89 e5 e8 38 00 00 00
[  260.020124] JIT code: 00000010: 31 c0 c9 c3
[  260.021229] flen=8 proglen=51 pass=1 image=ffffffffa000c164 from=kprobe-load pid=1568
[  260.023132] JIT code: 00000000: 0f 1f 44 00 00 66 90 55 48 89 e5 48 81 ec 08 00
[  260.025129] JIT code: 00000010: 00 00 48 bf 73 75 62 70 72 6f 67 00 48 89 7d f8
[  260.027199] JIT code: 00000020: 48 89 ef 48 83 c7 f8 be 08 00 00 00 e8 9e 19 1d
[  260.029226] JIT code: 00000030: e1 c9 c3
$ bpf_jit_disasm
51 bytes emitted from JIT compiler (pass:1, flen:8)
ffffffffa000c164 + <x>:
   0:   nopl   0x0(%rax,%rax,1)
   5:   xchg   %ax,%ax
   7:   push   %rbp
   8:   mov    %rsp,%rbp
   b:   sub    $0x8,%rsp
  12:   movabs $0x676f7270627573,%rdi
  1c:   mov    %rdi,-0x8(%rbp)
  20:   mov    %rbp,%rdi
  23:   add    $0xfffffffffffffff8,%rdi
  27:   mov    $0x8,%esi
  2c:   callq  0xffffffffe11d19cf
  31:   leaveq
  32:   retq
$ bpf_jit_disasm
20 bytes emitted from JIT compiler (pass:1, flen:3)
ffffffffa000c11c + <x>:
   0:   nopl   0x0(%rax,%rax,1)
   5:   xchg   %ax,%ax
   7:   push   %rbp
   8:   mov    %rsp,%rbp
   b:   callq  0x0000000000000048
  10:   xor    %eax,%eax
  12:   leaveq
  13:   retq
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc John Fastabend <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Hou Tao <[email protected]>
---
 arch/x86/net/bpf_jit_comp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/net/bpf_jit_comp.c +0 -3
index 36ffe67ad6e5..4e017102cc16 100644
@@ -2543,9 +2543,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
                cond_resched();
        }
 
-       if (bpf_jit_enable > 1)
-               bpf_jit_dump(prog->len, proglen, pass + 1, image);
-
        if (image) {
                if (!prog->is_func || extra_pass) {
                        /*

arch/x86/net/bpf_jit_comp.c struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) · line 2563

Quoted 6 lines
arch/x86/net/bpf_jit_comp.c +3 -0
@@ -2561,6 +2558,9 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
                                goto out_image;
                        }
 
+                       if (bpf_jit_enable > 1)
+                               bpf_jit_dump(prog->len, proglen, pass + 1, image);
+

bpf_jit_enable==2 is broken.
Please delete it from everywhere including docs.
Use bpftool prog dump instead.

#2 lore raw
Quoted 118 lines · Alexei Starovoitov
Quoted 116 lines · On Fri, Dec 30, 2022 at 3:38 AM <[email protected]> wrote:

From: Tonghao Zhang <[email protected]>

With bpf_jit_binary_pack_finalize invoked, we copy rw_header to header and then image/insn is valid.

Write a test BPF prog which include subprog:

$ llvm-objdump -d subprog.o
Disassembly of section .text:
0000000000000000 <subprog>:
       0:       18 01 00 00 73 75 62 70 00 00 00 00 72 6f 67 00 r1 = 29114459903653235 ll
       2:       7b 1a f8 ff 00 00 00 00 *(u64 *)(r10 - 8) = r1
       3:       bf a1 00 00 00 00 00 00 r1 = r10
       4:       07 01 00 00 f8 ff ff ff r1 += -8
       5:       b7 02 00 00 08 00 00 00 r2 = 8
       6:       85 00 00 00 06 00 00 00 call 6
       7:       95 00 00 00 00 00 00 00 exit
Disassembly of section raw_tp/sys_enter:
0000000000000000 <entry>:
       0:       85 10 00 00 ff ff ff ff call -1
       1:       b7 00 00 00 00 00 00 00 r0 = 0
       2:       95 00 00 00 00 00 00 00 exit

Without this patch, kernel print message:

[  580.775387] flen=8 proglen=51 pass=3 image=ffffffffa000c20c from=kprobe-load pid=1643
[  580.777236] JIT code: 00000000: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
[  580.779037] JIT code: 00000010: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
[  580.780767] JIT code: 00000020: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
[  580.782568] JIT code: 00000030: cc cc cc
$ bpf_jit_disasm
51 bytes emitted from JIT compiler (pass:3, flen:8)
ffffffffa000c20c + <x>:
   0:   int3
   1:   int3
   2:   int3
   3:   int3
   4:   int3
   5:   int3
   ...

To fix this issue:

[  260.016071] flen=3 proglen=20 pass=1 image=ffffffffa000c11c from=kprobe-load pid=1568
[  260.018094] JIT code: 00000000: 0f 1f 44 00 00 66 90 55 48 89 e5 e8 38 00 00 00
[  260.020124] JIT code: 00000010: 31 c0 c9 c3
[  260.021229] flen=8 proglen=51 pass=1 image=ffffffffa000c164 from=kprobe-load pid=1568
[  260.023132] JIT code: 00000000: 0f 1f 44 00 00 66 90 55 48 89 e5 48 81 ec 08 00
[  260.025129] JIT code: 00000010: 00 00 48 bf 73 75 62 70 72 6f 67 00 48 89 7d f8
[  260.027199] JIT code: 00000020: 48 89 ef 48 83 c7 f8 be 08 00 00 00 e8 9e 19 1d
[  260.029226] JIT code: 00000030: e1 c9 c3
$ bpf_jit_disasm
51 bytes emitted from JIT compiler (pass:1, flen:8)
ffffffffa000c164 + <x>:
   0:   nopl   0x0(%rax,%rax,1)
   5:   xchg   %ax,%ax
   7:   push   %rbp
   8:   mov    %rsp,%rbp
   b:   sub    $0x8,%rsp
  12:   movabs $0x676f7270627573,%rdi
  1c:   mov    %rdi,-0x8(%rbp)
  20:   mov    %rbp,%rdi
  23:   add    $0xfffffffffffffff8,%rdi
  27:   mov    $0x8,%esi
  2c:   callq  0xffffffffe11d19cf
  31:   leaveq
  32:   retq
$ bpf_jit_disasm
20 bytes emitted from JIT compiler (pass:1, flen:3)
ffffffffa000c11c + <x>:
   0:   nopl   0x0(%rax,%rax,1)
   5:   xchg   %ax,%ax
   7:   push   %rbp
   8:   mov    %rsp,%rbp
   b:   callq  0x0000000000000048
  10:   xor    %eax,%eax
  12:   leaveq
  13:   retq
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc John Fastabend <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Hou Tao <[email protected]>
---
 arch/x86/net/bpf_jit_comp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/net/bpf_jit_comp.c +0 -3
index 36ffe67ad6e5..4e017102cc16 100644
@@ -2543,9 +2543,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
                cond_resched();
        }
 
-       if (bpf_jit_enable > 1)
-               bpf_jit_dump(prog->len, proglen, pass + 1, image);
-
        if (image) {
                if (!prog->is_func || extra_pass) {
                        /*
@@ -2561,6 +2558,9 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
                                goto out_image;
                        }
 
Quoted 7 lines

arch/x86/net/bpf_jit_comp.c struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) · line 2563

Quoted 3 lines
arch/x86/net/bpf_jit_comp.c +3 -0
+                       if (bpf_jit_enable > 1)
+                               bpf_jit_dump(prog->len, proglen, pass + 1, image);
+

bpf_jit_enable==2 is broken.
Please delete it from everywhere including docs.
Use bpftool prog dump instead.

Thanks, send v2 soon.

Signature · 1 line
Best regards, Tonghao