BPF List
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Daniel Borkmann <daniel@iogearbox.net>,
	Quentin Monnet <quentin@isovalent.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>
Cc: "Martin KaFai Lau" <kafai@fb.com>,
	"Song Liu" <songliubraving@fb.com>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"KP Singh" <kpsingh@kernel.org>,
	"Stanislav Fomichev" <sdf@google.com>,
	"Hao Luo" <haoluo@google.com>, "Jiri Olsa" <jolsa@kernel.org>,
	bpf@vger.kernel.org,
	"Niklas Söderlund" <niklas.soderlund@corigine.com>,
	"Simon Horman" <simon.horman@corigine.com>
Subject: Re: [PATCH bpf-next v2 6/8] bpftool: Add LLVM as default library for disassembling JIT-ed programs
Date: Mon, 19 Sep 2022 21:08:04 -0700	[thread overview]
Message-ID: <60c18ba0-6f3a-4385-8622-9db8013dee28@fb.com> (raw)
In-Reply-To: <00d4de2e-c7ac-7aa5-9d31-868d73af4fe2@iogearbox.net>



On 9/16/22 2:09 PM, Daniel Borkmann wrote:
> On 9/11/22 10:14 PM, Quentin Monnet wrote:
>> To disassemble instructions for JIT-ed programs, bpftool has relied on
>> the libbfd library. This has been problematic in the past: libbfd's
>> interface is not meant to be stable and has changed several times. For
>> building bpftool, we have to detect how the libbfd version on the system
>> behaves, which is why we have to handle features disassembler-four-args
>> and disassembler-init-styled in the Makefile. When it comes to shipping
>> bpftool, this has also caused issues with several distribution
>> maintainers unwilling to support the feature (see for example Debian's
>> page for binutils-dev, which ships libbfd: "Note that building Debian
>> packages which depend on the shared libbfd is Not Allowed." [0]).
>>
>> For these reasons, we add support for LLVM as an alternative to libbfd
>> for disassembling instructions of JIT-ed programs. Thanks to the
>> preparation work in the previous commits, it's easy to add the library
>> by passing the relevant compilation options in the Makefile, and by
>> adding the functions for setting up the LLVM disassembler in file
>> jit_disasm.c.
> 
> Could you add more context around the LLVM lib? The motivation is that 
> libbfd's
> interface is not meant to be stable and has changed several times. How 
> does this
> look on the LLVM's library side? Also, for the 2nd part, what is 
> Debian's stance
> related to the LLVM lib? Would be good if both is explained in the 
> commit message.
> Right now it mainly reads 'that libbfd has all these issues, so we're 
> moving to
> something else', so would be good to provide more context to the ready 
> why the
> 'something else' is better than current one.

It will be good to mention that e.g., llvm development package
(e.g., llvm-devel for fedora) is needed for bpftool build with llvm.

> 
>> Naturally, the display of disassembled instructions comes with a few
>> minor differences. Here is a sample output with libbfd (already
>> supported before this patch):
>>
>>      # bpftool prog dump jited id 56
>>      bpf_prog_6deef7357e7b4530:
>>         0:   nopl   0x0(%rax,%rax,1)
>>         5:   xchg   %ax,%ax
>>         7:   push   %rbp
>>         8:   mov    %rsp,%rbp
>>         b:   push   %rbx
>>         c:   push   %r13
>>         e:   push   %r14
>>        10:   mov    %rdi,%rbx
>>        13:   movzwq 0xb4(%rbx),%r13
>>        1b:   xor    %r14d,%r14d
>>        1e:   or     $0x2,%r14d
>>        22:   mov    $0x1,%eax
>>        27:   cmp    $0x2,%r14
>>        2b:   jne    0x000000000000002f
>>        2d:   xor    %eax,%eax
>>        2f:   pop    %r14
>>        31:   pop    %r13
>>        33:   pop    %rbx
>>        34:   leave
>>        35:   ret
>>
>> LLVM supports several variants that we could set when initialising the
>> disassembler, for example with:
>>
>>      LLVMSetDisasmOptions(*ctx,
>>                           LLVMDisassembler_Option_AsmPrinterVariant);
>>
>> but the default printer is used for now. Here is the output with LLVM:
>>
>>      # bpftool prog dump jited id 56
>>      bpf_prog_6deef7357e7b4530:
>>         0:   nopl    (%rax,%rax)
>>         5:   nop
>>         7:   pushq   %rbp
>>         8:   movq    %rsp, %rbp
>>         b:   pushq   %rbx
>>         c:   pushq   %r13
>>         e:   pushq   %r14
>>        10:   movq    %rdi, %rbx
>>        13:   movzwq  180(%rbx), %r13
>>        1b:   xorl    %r14d, %r14d
>>        1e:   orl     $2, %r14d
>>        22:   movl    $1, %eax
>>        27:   cmpq    $2, %r14
>>        2b:   jne     0x2f
>>        2d:   xorl    %eax, %eax
>>        2f:   popq    %r14
>>        31:   popq    %r13
>>        33:   popq    %rbx
>>        34:   leave
>>        35:   retq
>>
>> The LLVM disassembler comes as the default choice, with libbfd as a
>> fall-back.
>>
>> Of course, we could replace libbfd entirely and avoid supporting two
>> different libraries. One reason for keeping libbfd is that, right now,
>> it works well, we have all we need in terms of features detection in the
>> Makefile, so it provides a fallback for disassembling JIT-ed programs if
>> libbfd is installed but LLVM is not. The other motivation is that libbfd
>> supports nfp instruction for Netronome's SmartNICs and can be used to
>> disassemble offloaded programs, something that LLVM cannot do. If
>> libbfd's interface breaks again in the future, we might reconsider
>> keeping support for it.
>>
>> [0] 
>> https://packages.debian.org/buster/binutils-dev
>> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
>> Tested-by: Niklas Söderlund <niklas.soderlund@corigine.com>
> 
> Thanks,
> Daniel

  reply	other threads:[~2022-09-20  4:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-11 20:14 [PATCH bpf-next v2 0/8] bpftool: Add LLVM as default library for disassembling JIT-ed programs Quentin Monnet
2022-09-11 20:14 ` [PATCH bpf-next v2 1/8] bpftool: Define _GNU_SOURCE only once Quentin Monnet
2022-09-11 20:14 ` [PATCH bpf-next v2 2/8] bpftool: Remove asserts from JIT disassembler Quentin Monnet
2022-09-11 20:14 ` [PATCH bpf-next v2 3/8] bpftool: Split FEATURE_TESTS/FEATURE_DISPLAY definitions in Makefile Quentin Monnet
2022-09-11 20:14 ` [PATCH bpf-next v2 4/8] bpftool: Group libbfd defs in Makefile, only pass them if we use libbfd Quentin Monnet
2022-09-11 20:14 ` [PATCH bpf-next v2 5/8] bpftool: Refactor disassembler for JIT-ed programs Quentin Monnet
2022-09-11 20:14 ` [PATCH bpf-next v2 6/8] bpftool: Add LLVM as default library for disassembling " Quentin Monnet
2022-09-16 21:09   ` Daniel Borkmann
2022-09-20  4:08     ` Yonghong Song [this message]
2022-09-23 11:23       ` Quentin Monnet
2022-09-23 11:23     ` Quentin Monnet
2022-09-19 23:58   ` Yonghong Song
2022-09-11 20:14 ` [PATCH bpf-next v2 7/8] bpftool: Support setting alternative arch for JIT disasm with LLVM Quentin Monnet
2022-09-11 20:14 ` [PATCH bpf-next v2 8/8] bpftool: Add llvm feature to "bpftool version" Quentin Monnet
2022-09-20  0:03   ` Yonghong Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=60c18ba0-6f3a-4385-8622-9db8013dee28@fb.com \
    --to=yhs@fb.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=niklas.soderlund@corigine.com \
    --cc=quentin@isovalent.com \
    --cc=sdf@google.com \
    --cc=simon.horman@corigine.com \
    --cc=songliubraving@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox