From patchwork Thu Oct 20 12:36:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13013392 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC525C433FE for ; Thu, 20 Oct 2022 12:37:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229874AbiJTMhY (ORCPT ); Thu, 20 Oct 2022 08:37:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230148AbiJTMhW (ORCPT ); Thu, 20 Oct 2022 08:37:22 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E776F13D19 for ; Thu, 20 Oct 2022 05:37:20 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id v11so4812129wmd.1 for ; Thu, 20 Oct 2022 05:37:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DJc2WTeQ79aN7lw9j3ViprJPP9RDB1+5PaPrZJopgzo=; b=jchz0dVKbvRcD+RevPINYGXa69CHh5VGIHdmd+IHrWx/dxqHNVTAFQgvMSO2X82i7o qCa6RqnAbWXAcnR16P1HtZDsYruPrF5QFzbuz3R+NHpLpD7LUUdnNc5OMdkgTq+BZBof LmyxRCfPRhcdceD8tr/4LA0OnAnl4QjtjngfMghEmi7UbLG6CW3zFdr2rLrYyG8Yjsq8 3d3rxqp3gUZetLscfWZo401bRBbyot2C68AIx6GjvJ/3lUiseXmMkHFQLEJMmlENAKj8 WikoXkVPuHmF05EyRhgKwXkSWhxeIEgMsMxZv1QSCR2sUfWKft7i7OpVLkDfCQJfcxuq OEDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DJc2WTeQ79aN7lw9j3ViprJPP9RDB1+5PaPrZJopgzo=; b=WWgPTb9cY2omF3YsIy2fl/FnhQGXzvfPukmB/rT1jWJN5ZS+ECsDqCGVcvWzFdu8pR KX/FKNAmO1p0iRvfRftvjMmHRBQgs/rfO8U1VWl5anOYM0snKImmEpuRZ0DcXEioiIfB TBGsmqU31AW9OjzNBl7F4tItvevjgl3W1euUEmWtcywWJQZFIpI4ZEqfltfpEPiagRnX fFr+DlJhmfM8Ob9QWo8PDM7/yjqZjZkVTqg8DbEjKjeYGZ8Cm2gW87VFCWc78oreGZfE wlZYH2ApbISet+9FUxbHnbRjiwFQIZVSN8KydacRICz09onW++G6OzEFYtFvUArjifjl 2qMQ== X-Gm-Message-State: ACrzQf2X5cJ2kIaCewbBJGrmAhtWOYQg5a6cx8QV7coTGKNVQx/XXi+r bhyu2h54OO93oEztTlGAXQPmaUA6tE7naFLq X-Google-Smtp-Source: AMsMyM5miSJidwxoHhTghzhHpYYBtis7zszr6XqNG4KQ8DYXk1Crs4FZ6MEJgU8KxH4gbJyZ9NwFmg== X-Received: by 2002:a05:600c:6885:b0:3bd:d782:623c with SMTP id fn5-20020a05600c688500b003bdd782623cmr9091693wmb.102.1666269439485; Thu, 20 Oct 2022 05:37:19 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id h10-20020a5d504a000000b0022a9246c853sm16329581wrt.41.2022.10.20.05.37.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Oct 2022 05:37:18 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet , Song Liu Subject: [PATCH bpf-next v3 1/8] bpftool: Define _GNU_SOURCE only once Date: Thu, 20 Oct 2022 13:36:57 +0100 Message-Id: <20221020123704.91203-2-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221020123704.91203-1-quentin@isovalent.com> References: <20221020123704.91203-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net _GNU_SOURCE is defined in several source files for bpftool, but only one of them takes the precaution of checking whether the value is already defined. Add #ifndef for other occurrences too. This is in preparation for the support of disassembling JIT-ed programs with LLVM, with $(llvm-config --cflags) passing -D_GNU_SOURCE as a compilation argument. Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Song Liu --- tools/bpf/bpftool/common.c | 2 ++ tools/bpf/bpftool/iter.c | 2 ++ tools/bpf/bpftool/jit_disasm.c | 2 ++ tools/bpf/bpftool/net.c | 2 ++ tools/bpf/bpftool/perf.c | 2 ++ tools/bpf/bpftool/prog.c | 2 ++ tools/bpf/bpftool/xlated_dumper.c | 2 ++ 7 files changed, 14 insertions(+) diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index 8727765add88..4c2e909a2d67 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include diff --git a/tools/bpf/bpftool/iter.c b/tools/bpf/bpftool/iter.c index f88fdc820d23..a3e6b167153d 100644 --- a/tools/bpf/bpftool/iter.c +++ b/tools/bpf/bpftool/iter.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) // Copyright (C) 2020 Facebook +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c index aaf99a0168c9..71cb258ab0ee 100644 --- a/tools/bpf/bpftool/jit_disasm.c +++ b/tools/bpf/bpftool/jit_disasm.c @@ -11,7 +11,9 @@ * Licensed under the GNU General Public License, version 2.0 (GPLv2) */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c index 526a332c48e6..c40e44c938ae 100644 --- a/tools/bpf/bpftool/net.c +++ b/tools/bpf/bpftool/net.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) // Copyright (C) 2018 Facebook +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include diff --git a/tools/bpf/bpftool/perf.c b/tools/bpf/bpftool/perf.c index 226ec2c39052..91743445e4c7 100644 --- a/tools/bpf/bpftool/perf.c +++ b/tools/bpf/bpftool/perf.c @@ -2,7 +2,9 @@ // Copyright (C) 2018 Facebook // Author: Yonghong Song +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index c81362a001ba..a31ae9f0c307 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c index 2d9cd6a7b3c8..6fe3134ae45d 100644 --- a/tools/bpf/bpftool/xlated_dumper.c +++ b/tools/bpf/bpftool/xlated_dumper.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) /* Copyright (C) 2018 Netronome Systems, Inc. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include From patchwork Thu Oct 20 12:36:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13013394 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BCC0FC4332F for ; Thu, 20 Oct 2022 12:37:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229552AbiJTMh0 (ORCPT ); Thu, 20 Oct 2022 08:37:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230137AbiJTMhY (ORCPT ); Thu, 20 Oct 2022 08:37:24 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00FBD275DE for ; Thu, 20 Oct 2022 05:37:21 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id t4so14785282wmj.5 for ; Thu, 20 Oct 2022 05:37:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ztgNoAY9JwrDyrrl7KRIcTiS50vpaEWtiAVvSexKhbQ=; b=wgGF22Qvm5v2glnTw6D1VaBgWTsz6jCW2W5WMS9yEuNViadbWgeJ8yrPx8xXr8+Sv9 eTJwHBkkNb/dSzeAsfODo85hXqjskduTCEUedwOU+ttmOHEnnJen6kxOdWYLn4IHo21E aSwYsiCU804zFhHvGjG4ImW0vTWMrE6O4hX/pKLkKGlj2ec8cvms89uoSDLUTj8HiSpZ J3bOf69L8b7YkFaE9730r3t/iwswXMG9GfR8n7zIHNPsTNPy68/9kvNEQB/vRbg165bo 48We4qnzz81zsfh3PiOvZau0SarwySIcGA/d2P7Zk7fpZdlgPGH476GJre5YX/U3SJQ6 GGWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ztgNoAY9JwrDyrrl7KRIcTiS50vpaEWtiAVvSexKhbQ=; b=ToRHUTuDyU8eTgBVLvUgabHEsryHnJsIJxYgd8sPXyvibrXmJXhZbAoReS0yUsoF2e RVgD7jnD5hzXEGMW0/+9cULISS3quH8EETjJLuTzGpE2NPZb7aeyFuqCZMfpUVz+S1xm HwnmWJKUOIlPUs3n92W1daISApwRKvenk1prf48Qs7MhaBRE3d41WVRebdsWAinFRa/B 3Neec7MWYv5QTj5aJju68rYvzMGu9CehiNj0nTOrvN+/4ZOg90YoJGTiUIqc+8Lzf3Hv VPu5ZQtvuAAIXrcxsBkp+mWhEBPkJ/CGiQlCp6yHcu6VuOnFhMY8TUjG/71xL0O4dDs5 dyhw== X-Gm-Message-State: ACrzQf2d+cfc3+kWdUyzdXYG0GZyLxArCjaYuYb3QVShuSOZORRovXa1 UfDckxKnUhAUQEySMUXBUb9xIQ== X-Google-Smtp-Source: AMsMyM7RlNXEK4iHileA1BsODjtfwZgZDbtoF33aMRODQIGDtgj9t5tTod7jI3CVvXTDHQyDa44gKQ== X-Received: by 2002:a05:600c:1e8b:b0:3c6:f6e5:c41d with SMTP id be11-20020a05600c1e8b00b003c6f6e5c41dmr14929732wmb.12.1666269440468; Thu, 20 Oct 2022 05:37:20 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id h10-20020a5d504a000000b0022a9246c853sm16329581wrt.41.2022.10.20.05.37.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Oct 2022 05:37:20 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet , Song Liu Subject: [PATCH bpf-next v3 2/8] bpftool: Remove asserts from JIT disassembler Date: Thu, 20 Oct 2022 13:36:58 +0100 Message-Id: <20221020123704.91203-3-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221020123704.91203-1-quentin@isovalent.com> References: <20221020123704.91203-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The JIT disassembler in bpftool is the only components (with the JSON writer) using asserts to check the return values of functions. But it does not do so in a consistent way, and diasm_print_insn() returns no value, although sometimes the operation failed. Remove the asserts, and instead check the return values, print messages on errors, and propagate the error to the caller from prog.c. Remove the inclusion of assert.h from jit_disasm.c, and also from map.c where it is unused. Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Song Liu --- tools/bpf/bpftool/jit_disasm.c | 51 +++++++++++++++++++++++----------- tools/bpf/bpftool/main.h | 25 +++++++++-------- tools/bpf/bpftool/map.c | 1 - tools/bpf/bpftool/prog.c | 15 ++++++---- 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c index 71cb258ab0ee..723a9b799a0c 100644 --- a/tools/bpf/bpftool/jit_disasm.c +++ b/tools/bpf/bpftool/jit_disasm.c @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -31,14 +30,18 @@ #include "json_writer.h" #include "main.h" -static void get_exec_path(char *tpath, size_t size) +static int get_exec_path(char *tpath, size_t size) { const char *path = "/proc/self/exe"; ssize_t len; len = readlink(path, tpath, size - 1); - assert(len > 0); + if (len <= 0) + return -1; + tpath[len] = 0; + + return 0; } static int oper_count; @@ -99,30 +102,39 @@ static int fprintf_json_styled(void *out, return r; } -void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, - const char *arch, const char *disassembler_options, - const struct btf *btf, - const struct bpf_prog_linfo *prog_linfo, - __u64 func_ksym, unsigned int func_idx, - bool linum) +int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, + const char *arch, const char *disassembler_options, + const struct btf *btf, + const struct bpf_prog_linfo *prog_linfo, + __u64 func_ksym, unsigned int func_idx, + bool linum) { const struct bpf_line_info *linfo = NULL; disassembler_ftype disassemble; + int count, i, pc = 0, err = -1; struct disassemble_info info; unsigned int nr_skip = 0; - int count, i, pc = 0; char tpath[PATH_MAX]; bfd *bfdf; if (!len) - return; + return -1; memset(tpath, 0, sizeof(tpath)); - get_exec_path(tpath, sizeof(tpath)); + if (get_exec_path(tpath, sizeof(tpath))) { + p_err("failed to create disasembler (get_exec_path)"); + return -1; + } bfdf = bfd_openr(tpath, NULL); - assert(bfdf); - assert(bfd_check_format(bfdf, bfd_object)); + if (!bfdf) { + p_err("failed to create disassembler (bfd_openr)"); + return -1; + } + if (!bfd_check_format(bfdf, bfd_object)) { + p_err("failed to create disassembler (bfd_check_format)"); + goto exit_close; + } if (json_output) init_disassemble_info_compat(&info, stdout, @@ -141,7 +153,7 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, bfdf->arch_info = inf; } else { p_err("No libbfd support for %s", arch); - return; + goto exit_close; } } @@ -162,7 +174,10 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, #else disassemble = disassembler(bfdf); #endif - assert(disassemble); + if (!disassemble) { + p_err("failed to create disassembler"); + goto exit_close; + } if (json_output) jsonw_start_array(json_wtr); @@ -226,7 +241,11 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, if (json_output) jsonw_end_array(json_wtr); + err = 0; + +exit_close: bfd_close(bfdf); + return err; } int disasm_init(void) diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index 5e5060c2ac04..c9e171082cf6 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -173,22 +173,23 @@ int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len); struct bpf_prog_linfo; #ifdef HAVE_LIBBFD_SUPPORT -void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, - const char *arch, const char *disassembler_options, - const struct btf *btf, - const struct bpf_prog_linfo *prog_linfo, - __u64 func_ksym, unsigned int func_idx, - bool linum); +int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, + const char *arch, const char *disassembler_options, + const struct btf *btf, + const struct bpf_prog_linfo *prog_linfo, + __u64 func_ksym, unsigned int func_idx, + bool linum); int disasm_init(void); #else static inline -void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, - const char *arch, const char *disassembler_options, - const struct btf *btf, - const struct bpf_prog_linfo *prog_linfo, - __u64 func_ksym, unsigned int func_idx, - bool linum) +int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, + const char *arch, const char *disassembler_options, + const struct btf *btf, + const struct bpf_prog_linfo *prog_linfo, + __u64 func_ksym, unsigned int func_idx, + bool linum) { + return 0; } static inline int disasm_init(void) { diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 9a6ca9f31133..3087ced658ad 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -1,7 +1,6 @@ // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ -#include #include #include #include diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index a31ae9f0c307..345dca656a34 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -822,10 +822,11 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, printf("%s:\n", sym_name); } - disasm_print_insn(img, lens[i], opcodes, - name, disasm_opt, btf, - prog_linfo, ksyms[i], i, - linum); + if (disasm_print_insn(img, lens[i], opcodes, + name, disasm_opt, btf, + prog_linfo, ksyms[i], i, + linum)) + goto exit_free; img += lens[i]; @@ -838,8 +839,10 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, if (json_output) jsonw_end_array(json_wtr); } else { - disasm_print_insn(buf, member_len, opcodes, name, - disasm_opt, btf, NULL, 0, 0, false); + if (disasm_print_insn(buf, member_len, opcodes, name, + disasm_opt, btf, NULL, 0, 0, + false)) + goto exit_free; } } else if (visual) { if (json_output) From patchwork Thu Oct 20 12:36:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13013393 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD0E5C43217 for ; Thu, 20 Oct 2022 12:37:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229794AbiJTMhZ (ORCPT ); Thu, 20 Oct 2022 08:37:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229552AbiJTMhY (ORCPT ); Thu, 20 Oct 2022 08:37:24 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A446495CD for ; Thu, 20 Oct 2022 05:37:23 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id l16-20020a05600c4f1000b003c6c0d2a445so2059092wmq.4 for ; Thu, 20 Oct 2022 05:37:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ixQHMERYdgRb7FUM1/1Io76g7S2tfmEKG93IxR6PYMU=; b=3UQpyJ4RSqP6U+LqFymJjrN/MX4HRMwY4itEvcF1El5nHMUpGuZka79djeTDUWMuFc yvSGPFpBMoyazKgTLI5KxyxDyxOoSjCVvq8ZJxCxrt5uDIyGcuKYbhVPRYY4oWaITj1s U+pvM86TSSZEDC17Fj5DY3M1JH5UakiI/EL64myfDPb5jGS7JhvcASDdPoLSJpiRfToy s8eAWsoG6fwUrtYIE2eDBuZC9ez61s1yyvbSMz6SPOiZ1YmBoXAvLQAkoccLizqQTmy+ ie9qk4V3hS0Mv4euhrtEOz0Gte51QHvSJpkEr+hT6NUyQw/09RMNXYTbJjL+tiVrrJ50 4Ksg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ixQHMERYdgRb7FUM1/1Io76g7S2tfmEKG93IxR6PYMU=; b=k7HZWdC9O75EM+NNmlTEXZSUBU5gBG8HYmCWuZxfPOPkucizGBOBjIrl9sL6uBAqxs N1ZlLbP2QEkxvQPj7SaZ4tg9fp6uAvUfRayNoPQNriWdJSbjinBwn/wCo+vrDHmMPhe2 QdcmateMLT63UyH63YjaGYNNEkFsCUyyDT0SI0C4fuKVJMeS7SD5YDo/H7YbUpBROojl 07fy0mcSEbExgBH7vQNELkXYdOXQsQxFaVik04R1JfmZk9HzMP9ncXZbLPY5Vnrk958h CpkY1t2sb5WVxL79J+OS/UFZ7elW14/KL6qE8jD31xhv9Z9EN8ZxoKIl90+ggJ04dw7Y bkvQ== X-Gm-Message-State: ACrzQf1K8oIR9/gFcfaS/M8qLKNm1z55SrLrpTLxII+q+6mp9SeCg8il MeNKtac3OYdXrta/9cdtE38HjQ== X-Google-Smtp-Source: AMsMyM4+2pDOsfQhWiLHK+tWKSZySmZIjXUgoVB1aMiZHpb1cpB698IqsmKBWvR8xL9Nw33IoVeJ7A== X-Received: by 2002:a1c:7407:0:b0:3c6:cc25:c02f with SMTP id p7-20020a1c7407000000b003c6cc25c02fmr9016493wmc.124.1666269441420; Thu, 20 Oct 2022 05:37:21 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id h10-20020a5d504a000000b0022a9246c853sm16329581wrt.41.2022.10.20.05.37.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Oct 2022 05:37:21 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet , Andres Freund , Song Liu Subject: [PATCH bpf-next v3 3/8] bpftool: Split FEATURE_TESTS/FEATURE_DISPLAY definitions in Makefile Date: Thu, 20 Oct 2022 13:36:59 +0100 Message-Id: <20221020123704.91203-4-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221020123704.91203-1-quentin@isovalent.com> References: <20221020123704.91203-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Make FEATURE_TESTS and FEATURE_DISPLAY easier to read and less likely to be subject to conflicts on updates by having one feature per line. Suggested-by: Andres Freund Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Song Liu --- tools/bpf/bpftool/Makefile | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 4a95c017ad4c..0218d6a1cae7 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile @@ -93,11 +93,20 @@ INSTALL ?= install RM ?= rm -f FEATURE_USER = .bpftool -FEATURE_TESTS = libbfd libbfd-liberty libbfd-liberty-z \ - disassembler-four-args disassembler-init-styled libcap \ - clang-bpf-co-re -FEATURE_DISPLAY = libbfd libbfd-liberty libbfd-liberty-z \ - libcap clang-bpf-co-re + +FEATURE_TESTS := clang-bpf-co-re +FEATURE_TESTS += libcap +FEATURE_TESTS += libbfd +FEATURE_TESTS += libbfd-liberty +FEATURE_TESTS += libbfd-liberty-z +FEATURE_TESTS += disassembler-four-args +FEATURE_TESTS += disassembler-init-styled + +FEATURE_DISPLAY := clang-bpf-co-re +FEATURE_DISPLAY += libcap +FEATURE_DISPLAY += libbfd +FEATURE_DISPLAY += libbfd-liberty +FEATURE_DISPLAY += libbfd-liberty-z check_feat := 1 NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall From patchwork Thu Oct 20 12:37:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13013395 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 499E1C433FE for ; Thu, 20 Oct 2022 12:37:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230056AbiJTMh0 (ORCPT ); Thu, 20 Oct 2022 08:37:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229988AbiJTMhZ (ORCPT ); Thu, 20 Oct 2022 08:37:25 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12265495C4 for ; Thu, 20 Oct 2022 05:37:24 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id c7-20020a05600c0ac700b003c6cad86f38so2219707wmr.2 for ; Thu, 20 Oct 2022 05:37:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pwBcXEdZ0bwNQEQkHCPZ0SYXvbbBEOobN7rGFCnE0t8=; b=rPYOO+ZPvSnE+WW2aBWc5QyV5/Ez2emewree60lbS76mGBNwSTQoLt+Nxo9PkMlg5B wSF+88tlniJrbtLFZ4b4kqRnFwc8cZHJvXuLFCDsn+olHFWtzqquQ2g+OYAeGpWWdTgm 1PigaWIqIGc6RKWiW+R8KqQvRrlnYi7v7rZQthvGOMuAKMBvPbTRGJvDQfrTvMOjv3cl XwXZHgbaTOL+T5ZQIPk8vbCOa/4fhZgtw0OhW/q3jwraXp+MSQOYrBpZ8Y89MFru8mgw 4S9SYuUIO+1ctH0NWbkY+Rhx6NQuVJxB50R+cLvMKGGZzTy4A9ACZWuGHpi+HEaP3nU4 saSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pwBcXEdZ0bwNQEQkHCPZ0SYXvbbBEOobN7rGFCnE0t8=; b=vRTj4PUgLSudzqm/AHIuRh1JWB8PkD8QVf3PsouAFGGHcZfJZjqsCpVvJPp3mn8O7F Sld8RBsnSiLuooUeI+XdCyQ/FKoIgotsSalkGDrdzoaVuH6e88tjjhAZODBLx+3DPQlj PJudfw420m7aFN7KkpwNomL32WWgukLZDVaizX0XbJ8QI2RFDMxwn/Km/tA2BJF5I2Z5 lhJsCP9ph2NpcnfM4rj+avAOoS/JGZBoA6I/4C7Mkg3uZ1VDMTtk6iKoFpuI0z5K7fQU 6WsT44hjdAfG0lnPcgZoPqZynpLpbfBSWT4bqj90pJWJe4g9JZwXfs5PUVrsPuwrTtW7 mcVA== X-Gm-Message-State: ACrzQf0aR3El2aTcGP57qIP8XUU9TAAU95Q4J11BTJXoX5XhxjN9hhfZ vLz7PjO0KzfYWBz5wQJxXEP/ww== X-Google-Smtp-Source: AMsMyM7QOTuCOC90uy7lWYb1z5U9woT5zewUp5PRRFJ1jY8w7kFCMa9RDpFdztd53UtTuKakDnDlqA== X-Received: by 2002:a1c:f20e:0:b0:3c2:5062:4017 with SMTP id s14-20020a1cf20e000000b003c250624017mr29276700wmc.175.1666269442396; Thu, 20 Oct 2022 05:37:22 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id h10-20020a5d504a000000b0022a9246c853sm16329581wrt.41.2022.10.20.05.37.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Oct 2022 05:37:22 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet , Song Liu Subject: [PATCH bpf-next v3 4/8] bpftool: Group libbfd defs in Makefile, only pass them if we use libbfd Date: Thu, 20 Oct 2022 13:37:00 +0100 Message-Id: <20221020123704.91203-5-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221020123704.91203-1-quentin@isovalent.com> References: <20221020123704.91203-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Bpftool uses libbfd for disassembling JIT-ed programs. But the feature is optional, and the tool can be compiled without libbfd support. The Makefile sets the relevant variables accordingly. It also sets variables related to libbfd's interface, given that it has changed over time. Group all those libbfd-related definitions so that it's easier to understand what we are testing for, and only use variables related to libbfd's interface if we need libbfd in the first place. In addition to make the Makefile clearer, grouping the definitions related to disassembling JIT-ed programs will help support alternatives to libbfd. Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Song Liu --- tools/bpf/bpftool/Makefile | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 0218d6a1cae7..1c81f4d514bb 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile @@ -124,13 +124,6 @@ include $(FEATURES_DUMP) endif endif -ifeq ($(feature-disassembler-four-args), 1) -CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE -endif -ifeq ($(feature-disassembler-init-styled), 1) - CFLAGS += -DDISASM_INIT_STYLED -endif - LIBS = $(LIBBPF) -lelf -lz LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz ifeq ($(feature-libcap), 1) @@ -142,9 +135,7 @@ include $(wildcard $(OUTPUT)*.d) all: $(OUTPUT)bpftool -BFD_SRCS = jit_disasm.c - -SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c)) +SRCS := $(wildcard *.c) ifeq ($(feature-libbfd),1) LIBS += -lbfd -ldl -lopcodes @@ -154,9 +145,21 @@ else ifeq ($(feature-libbfd-liberty-z),1) LIBS += -lbfd -ldl -lopcodes -liberty -lz endif +# If one of the above feature combinations is set, we support libbfd ifneq ($(filter -lbfd,$(LIBS)),) -CFLAGS += -DHAVE_LIBBFD_SUPPORT -SRCS += $(BFD_SRCS) + CFLAGS += -DHAVE_LIBBFD_SUPPORT + + # Libbfd interface changed over time, figure out what we need + ifeq ($(feature-disassembler-four-args), 1) + CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE + endif + ifeq ($(feature-disassembler-init-styled), 1) + CFLAGS += -DDISASM_INIT_STYLED + endif +endif +ifeq ($(filter -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),) + # No support for JIT disassembly + SRCS := $(filter-out jit_disasm.c,$(SRCS)) endif HOST_CFLAGS = $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\ From patchwork Thu Oct 20 12:37:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13013396 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 98F42C43219 for ; Thu, 20 Oct 2022 12:37:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230148AbiJTMh2 (ORCPT ); Thu, 20 Oct 2022 08:37:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229886AbiJTMh0 (ORCPT ); Thu, 20 Oct 2022 08:37:26 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D75E3495C8 for ; Thu, 20 Oct 2022 05:37:24 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id j7so34205466wrr.3 for ; Thu, 20 Oct 2022 05:37:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RA9eacgGZiA4SnV5uyL526OIKeHyinf6gpH6AIvYlko=; b=enyEii4aDYL369wDE9Z1eEQbOutcxsKsSj+TwCbSAPRuIzBTg/aGwic3MBFgVAFlg3 soLTkCekCEEsD2+3+GO81JgYqULtF0yUd9i6zhsyzGV6zXHywnyXyKgrxhfupQGsDXkT rox+lbqsM6CxWZXFuKYH56ySiK/dTrPvl3MgI8zV7eX97fkHEgpSlt+D7yx/YY3+6eRv 9F9tQpSt/oY59aSt09bd3BbtvY+AZWtm96LpDkkc3Hnuj+FQFx9eH1sTijJxccf5jd0Z f5BhBvLE/r1tdH3ayfmNqdSA009o5rloapx5EOJa2Y5tkWHvT9dYIJLwz8zrYzqsTjHW 7fJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RA9eacgGZiA4SnV5uyL526OIKeHyinf6gpH6AIvYlko=; b=kqOqvMUk3tJeZwjUkscEUvD2k3OQTlb5rV05VcyhVjiAZAkkQ04G1JQ4iNH20q4p+u G783DZ7ahws3n8V11Uem+M0HDDodaDz52cmVR6HXwIAEs5D26aW6IOSe4a5YKVE0hc2L Uth21qm2R0vf7C+nY80e27AvX85Xn160Istl96wig90eubCOeD036AC1FDRDmHl8ey9k w9S2ZTCd6wwo3EaQTHdq0S2IBz9rGbJpV4M0B+kYxnbzYEoVeyUDmPO0pM/G3F/JIgfw 4kdABhtIACyrLAHHTsAx73ndf0AskKLy8+PRGwa30yxB/nCM+BSC49nZaX8Pyx3P1A1t QBYQ== X-Gm-Message-State: ACrzQf0nA+TLPE2Uu5YOPV3q4VoumkuyHqTlG7FjKhrYWe8uDgXKyEhb JZ9SJ0fV3ywPu3eNw/I5GwzLeg== X-Google-Smtp-Source: AMsMyM6mfzCkNnXmHmUC0LxQN9ZpXPxzViP7555v55NPqG6BBBJfTZdbMP0qnQSlPsPMNNImIVDXuA== X-Received: by 2002:a05:6000:18ac:b0:235:6c05:1b90 with SMTP id b12-20020a05600018ac00b002356c051b90mr2736338wri.53.1666269443239; Thu, 20 Oct 2022 05:37:23 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id h10-20020a5d504a000000b0022a9246c853sm16329581wrt.41.2022.10.20.05.37.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Oct 2022 05:37:22 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet , Song Liu Subject: [PATCH bpf-next v3 5/8] bpftool: Refactor disassembler for JIT-ed programs Date: Thu, 20 Oct 2022 13:37:01 +0100 Message-Id: <20221020123704.91203-6-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221020123704.91203-1-quentin@isovalent.com> References: <20221020123704.91203-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Refactor disasm_print_insn() to extract the code specific to libbfd and move it to dedicated functions. There is no functional change. This is in preparation for supporting an alternative library for disassembling the instructions. Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Song Liu --- tools/bpf/bpftool/jit_disasm.c | 133 ++++++++++++++++++++++----------- 1 file changed, 88 insertions(+), 45 deletions(-) diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c index 723a9b799a0c..e31ad3950fd6 100644 --- a/tools/bpf/bpftool/jit_disasm.c +++ b/tools/bpf/bpftool/jit_disasm.c @@ -30,6 +30,14 @@ #include "json_writer.h" #include "main.h" +static int oper_count; + +typedef struct { + struct disassemble_info *info; + disassembler_ftype disassemble; + bfd *bfdf; +} disasm_ctx_t; + static int get_exec_path(char *tpath, size_t size) { const char *path = "/proc/self/exe"; @@ -44,7 +52,6 @@ static int get_exec_path(char *tpath, size_t size) return 0; } -static int oper_count; static int printf_json(void *out, const char *fmt, va_list ap) { char *s; @@ -102,46 +109,44 @@ static int fprintf_json_styled(void *out, return r; } -int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, - const char *arch, const char *disassembler_options, - const struct btf *btf, - const struct bpf_prog_linfo *prog_linfo, - __u64 func_ksym, unsigned int func_idx, - bool linum) +static int init_context(disasm_ctx_t *ctx, const char *arch, + const char *disassembler_options, + unsigned char *image, ssize_t len) { - const struct bpf_line_info *linfo = NULL; - disassembler_ftype disassemble; - int count, i, pc = 0, err = -1; - struct disassemble_info info; - unsigned int nr_skip = 0; + struct disassemble_info *info; char tpath[PATH_MAX]; bfd *bfdf; - if (!len) - return -1; - memset(tpath, 0, sizeof(tpath)); if (get_exec_path(tpath, sizeof(tpath))) { p_err("failed to create disasembler (get_exec_path)"); return -1; } - bfdf = bfd_openr(tpath, NULL); - if (!bfdf) { + ctx->bfdf = bfd_openr(tpath, NULL); + if (!ctx->bfdf) { p_err("failed to create disassembler (bfd_openr)"); return -1; } - if (!bfd_check_format(bfdf, bfd_object)) { + if (!bfd_check_format(ctx->bfdf, bfd_object)) { p_err("failed to create disassembler (bfd_check_format)"); - goto exit_close; + goto err_close; } + bfdf = ctx->bfdf; + + ctx->info = malloc(sizeof(struct disassemble_info)); + if (!ctx->info) { + p_err("mem alloc failed"); + goto err_close; + } + info = ctx->info; if (json_output) - init_disassemble_info_compat(&info, stdout, + init_disassemble_info_compat(info, stdout, (fprintf_ftype) fprintf_json, fprintf_json_styled); else - init_disassemble_info_compat(&info, stdout, + init_disassemble_info_compat(info, stdout, (fprintf_ftype) fprintf, fprintf_styled); @@ -153,31 +158,76 @@ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, bfdf->arch_info = inf; } else { p_err("No libbfd support for %s", arch); - goto exit_close; + goto err_free; } } - info.arch = bfd_get_arch(bfdf); - info.mach = bfd_get_mach(bfdf); + info->arch = bfd_get_arch(bfdf); + info->mach = bfd_get_mach(bfdf); if (disassembler_options) - info.disassembler_options = disassembler_options; - info.buffer = image; - info.buffer_length = len; + info->disassembler_options = disassembler_options; + info->buffer = image; + info->buffer_length = len; - disassemble_init_for_target(&info); + disassemble_init_for_target(info); #ifdef DISASM_FOUR_ARGS_SIGNATURE - disassemble = disassembler(info.arch, - bfd_big_endian(bfdf), - info.mach, - bfdf); + ctx->disassemble = disassembler(info->arch, + bfd_big_endian(bfdf), + info->mach, + bfdf); #else - disassemble = disassembler(bfdf); + ctx->disassemble = disassembler(bfdf); #endif - if (!disassemble) { + if (!ctx->disassemble) { p_err("failed to create disassembler"); - goto exit_close; + goto err_free; } + return 0; + +err_free: + free(info); +err_close: + bfd_close(ctx->bfdf); + return -1; +} + +static void destroy_context(disasm_ctx_t *ctx) +{ + free(ctx->info); + bfd_close(ctx->bfdf); +} + +static int +disassemble_insn(disasm_ctx_t *ctx, __maybe_unused unsigned char *image, + __maybe_unused ssize_t len, int pc) +{ + return ctx->disassemble(pc, ctx->info); +} + +int disasm_init(void) +{ + bfd_init(); + return 0; +} + +int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, + const char *arch, const char *disassembler_options, + const struct btf *btf, + const struct bpf_prog_linfo *prog_linfo, + __u64 func_ksym, unsigned int func_idx, + bool linum) +{ + const struct bpf_line_info *linfo = NULL; + unsigned int nr_skip = 0; + int count, i, pc = 0; + disasm_ctx_t ctx; + + if (!len) + return -1; + + if (init_context(&ctx, arch, disassembler_options, image, len)) + return -1; if (json_output) jsonw_start_array(json_wtr); @@ -205,7 +255,8 @@ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, printf("%4x:\t", pc); } - count = disassemble(pc, &info); + count = disassemble_insn(&ctx, image, len, pc); + if (json_output) { /* Operand array, was started in fprintf_json. Before * that, make sure we have a _null_ value if no operand @@ -241,15 +292,7 @@ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, if (json_output) jsonw_end_array(json_wtr); - err = 0; + destroy_context(&ctx); -exit_close: - bfd_close(bfdf); - return err; -} - -int disasm_init(void) -{ - bfd_init(); return 0; } From patchwork Thu Oct 20 12:37:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13013397 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00D78C4332F for ; Thu, 20 Oct 2022 12:37:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230164AbiJTMh3 (ORCPT ); Thu, 20 Oct 2022 08:37:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230093AbiJTMh2 (ORCPT ); Thu, 20 Oct 2022 08:37:28 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5A1946609 for ; Thu, 20 Oct 2022 05:37:25 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id j7so34205553wrr.3 for ; Thu, 20 Oct 2022 05:37:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mQ5G+LqCu4UgAdrOZLgN0oH7PzZxkUftVl9Lp5qe/AY=; b=oibsX0NEt50naCOeWeMiCic3/GlE56yp+BA0lgcpYMXtocwhtI+OxJvplo85FddpZ8 x99XgMK9hTAS+VfkItPnKSmL5WlMikaelN6XImAqJVMgDn02G0CnXXQX9qeu9LFniHQg GJDC0NlJJWXedTP2Utlmhbv0UGb390QlxphU7AKHV78boRHoPxFiobNa0OB5o5FeelK8 SSou9a8ydURQagzl26Ko76tJ2xR7Tf7FHYdXbtri3EKRVEjL0HrJ2w0aA6w9cBRW7oug 6pCGcf2JQ2ajWUzcxhNpCj75opNKcDhhFHsNE1VYxSHE0TBYvRaOfTC4StwYBx4vyYg6 LCPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mQ5G+LqCu4UgAdrOZLgN0oH7PzZxkUftVl9Lp5qe/AY=; b=tUCDjo+/zyOpfzg4W7pWXJKtbhsl1ptAktKteuJejrwsE5mu25BZX72czCpWW11Ddt 9frAe/n+Gr2KY2VtWayhSomSgSKcPE7XKL2HJrvAeQRd3KLfF71CxLXkjakfIHp5Mn7G 3cY1DtbPHuOm/Kf9kiv5UNG19jbIWWI92HBIU8EpZGOpHNcrD0RsmmE2h97G/AQpPHW1 pI3RWabMGpuTAy7jWzEWC+63ui0R7xnnvwGqni/hXVoMbOMT6+YK7LxvHhLw0ZBmQVqF LqoOtMdSPxPGI3mhtOckbVm7nagy5BmwZAq9m2xAtSJQQt+U1Kp4VMgqUSK5pcl9GsIy nruQ== X-Gm-Message-State: ACrzQf2HSUDRzIpNMCrNFeTyWdxCLSeVAn/TdM4xlh7fH2XowzqUfwtr Y4qPJ0/9mhafX/P3sGuaux1mRg== X-Google-Smtp-Source: AMsMyM7RsmCIHN6Pk8dWhd4enI8qUszktKMdNYI1QIJ5PSZhngHvGIRGggE7BvR5ouTIWR8rYScohw== X-Received: by 2002:adf:ce90:0:b0:236:4cd6:6a4c with SMTP id r16-20020adfce90000000b002364cd66a4cmr283647wrn.348.1666269444269; Thu, 20 Oct 2022 05:37:24 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id h10-20020a5d504a000000b0022a9246c853sm16329581wrt.41.2022.10.20.05.37.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Oct 2022 05:37:23 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet Subject: [PATCH bpf-next v3 6/8] bpftool: Add LLVM as default library for disassembling JIT-ed programs Date: Thu, 20 Oct 2022 13:37:02 +0100 Message-Id: <20221020123704.91203-7-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221020123704.91203-1-quentin@isovalent.com> References: <20221020123704.91203-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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. The LLVM disassembler requires the LLVM development package (usually llvm-dev or llvm-devel). The expectation is that the interface for this disassembler will be more stable. There is a note in LLVM's Developer Policy [1] stating that the stability for the C API is "best effort" and not guaranteed, but at least there is some effort to keep compatibility when possible (which hasn't really been the case for libbfd so far). Furthermore, the Debian page for the related LLVM package does not caution against linking to the lib, as binutils-dev page does. 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 [1] https://llvm.org/docs/DeveloperPolicy.html#c-api-changes Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Yonghong Song --- tools/bpf/bpftool/Makefile | 46 +++++++++----- tools/bpf/bpftool/jit_disasm.c | 112 ++++++++++++++++++++++++++++++++- tools/bpf/bpftool/main.h | 4 +- 3 files changed, 140 insertions(+), 22 deletions(-) diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 1c81f4d514bb..787b857d3fb5 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile @@ -95,6 +95,7 @@ RM ?= rm -f FEATURE_USER = .bpftool FEATURE_TESTS := clang-bpf-co-re +FEATURE_TESTS += llvm FEATURE_TESTS += libcap FEATURE_TESTS += libbfd FEATURE_TESTS += libbfd-liberty @@ -103,6 +104,7 @@ FEATURE_TESTS += disassembler-four-args FEATURE_TESTS += disassembler-init-styled FEATURE_DISPLAY := clang-bpf-co-re +FEATURE_DISPLAY += llvm FEATURE_DISPLAY += libcap FEATURE_DISPLAY += libbfd FEATURE_DISPLAY += libbfd-liberty @@ -137,27 +139,37 @@ all: $(OUTPUT)bpftool SRCS := $(wildcard *.c) -ifeq ($(feature-libbfd),1) - LIBS += -lbfd -ldl -lopcodes -else ifeq ($(feature-libbfd-liberty),1) - LIBS += -lbfd -ldl -lopcodes -liberty -else ifeq ($(feature-libbfd-liberty-z),1) - LIBS += -lbfd -ldl -lopcodes -liberty -lz -endif +ifeq ($(feature-llvm),1) + # If LLVM is available, use it for JIT disassembly + CFLAGS += -DHAVE_LLVM_SUPPORT + LLVM_CONFIG_LIB_COMPONENTS := mcdisassembler all-targets + CFLAGS += $(shell $(LLVM_CONFIG) --cflags --libs $(LLVM_CONFIG_LIB_COMPONENTS)) + LIBS += $(shell $(LLVM_CONFIG) --libs $(LLVM_CONFIG_LIB_COMPONENTS)) + LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags) +else + # Fall back on libbfd + ifeq ($(feature-libbfd),1) + LIBS += -lbfd -ldl -lopcodes + else ifeq ($(feature-libbfd-liberty),1) + LIBS += -lbfd -ldl -lopcodes -liberty + else ifeq ($(feature-libbfd-liberty-z),1) + LIBS += -lbfd -ldl -lopcodes -liberty -lz + endif -# If one of the above feature combinations is set, we support libbfd -ifneq ($(filter -lbfd,$(LIBS)),) - CFLAGS += -DHAVE_LIBBFD_SUPPORT + # If one of the above feature combinations is set, we support libbfd + ifneq ($(filter -lbfd,$(LIBS)),) + CFLAGS += -DHAVE_LIBBFD_SUPPORT - # Libbfd interface changed over time, figure out what we need - ifeq ($(feature-disassembler-four-args), 1) - CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE - endif - ifeq ($(feature-disassembler-init-styled), 1) - CFLAGS += -DDISASM_INIT_STYLED + # Libbfd interface changed over time, figure out what we need + ifeq ($(feature-disassembler-four-args), 1) + CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE + endif + ifeq ($(feature-disassembler-init-styled), 1) + CFLAGS += -DDISASM_INIT_STYLED + endif endif endif -ifeq ($(filter -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),) +ifeq ($(filter -DHAVE_LLVM_SUPPORT -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),) # No support for JIT disassembly SRCS := $(filter-out jit_disasm.c,$(SRCS)) endif diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c index e31ad3950fd6..c28b21f90cb9 100644 --- a/tools/bpf/bpftool/jit_disasm.c +++ b/tools/bpf/bpftool/jit_disasm.c @@ -20,18 +20,123 @@ #include #include #include -#include -#include #include #include #include + +#ifdef HAVE_LLVM_SUPPORT +#include +#include +#include +#include +#endif + +#ifdef HAVE_LIBBFD_SUPPORT +#include +#include #include +#endif #include "json_writer.h" #include "main.h" static int oper_count; +#ifdef HAVE_LLVM_SUPPORT +#define DISASM_SPACER + +typedef LLVMDisasmContextRef disasm_ctx_t; + +static int printf_json(char *s) +{ + s = strtok(s, " \t"); + jsonw_string_field(json_wtr, "operation", s); + + jsonw_name(json_wtr, "operands"); + jsonw_start_array(json_wtr); + oper_count = 1; + + while ((s = strtok(NULL, " \t,()")) != 0) { + jsonw_string(json_wtr, s); + oper_count++; + } + return 0; +} + +/* This callback to set the ref_type is necessary to have the LLVM disassembler + * print PC-relative addresses instead of byte offsets for branch instruction + * targets. + */ +static const char * +symbol_lookup_callback(__maybe_unused void *disasm_info, + __maybe_unused uint64_t ref_value, + uint64_t *ref_type, __maybe_unused uint64_t ref_PC, + __maybe_unused const char **ref_name) +{ + *ref_type = LLVMDisassembler_ReferenceType_InOut_None; + return NULL; +} + +static int +init_context(disasm_ctx_t *ctx, const char *arch, + __maybe_unused const char *disassembler_options, + __maybe_unused unsigned char *image, __maybe_unused ssize_t len) +{ + char *triple; + + if (arch) { + p_err("Architecture %s not supported", arch); + return -1; + } + + triple = LLVMGetDefaultTargetTriple(); + if (!triple) { + p_err("Failed to retrieve triple"); + return -1; + } + *ctx = LLVMCreateDisasm(triple, NULL, 0, NULL, symbol_lookup_callback); + LLVMDisposeMessage(triple); + + if (!*ctx) { + p_err("Failed to create disassembler"); + return -1; + } + + return 0; +} + +static void destroy_context(disasm_ctx_t *ctx) +{ + LLVMDisposeMessage(*ctx); +} + +static int +disassemble_insn(disasm_ctx_t *ctx, unsigned char *image, ssize_t len, int pc) +{ + char buf[256]; + int count; + + count = LLVMDisasmInstruction(*ctx, image + pc, len - pc, pc, + buf, sizeof(buf)); + if (json_output) + printf_json(buf); + else + printf("%s", buf); + + return count; +} + +int disasm_init(void) +{ + LLVMInitializeNativeTarget(); + LLVMInitializeNativeDisassembler(); + return 0; +} +#endif /* HAVE_LLVM_SUPPORT */ + +#ifdef HAVE_LIBBFD_SUPPORT +#define DISASM_SPACER "\t" + typedef struct { struct disassemble_info *info; disassembler_ftype disassemble; @@ -210,6 +315,7 @@ int disasm_init(void) bfd_init(); return 0; } +#endif /* HAVE_LIBBPFD_SUPPORT */ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, const char *arch, const char *disassembler_options, @@ -252,7 +358,7 @@ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, if (linfo) btf_dump_linfo_plain(btf, linfo, "; ", linum); - printf("%4x:\t", pc); + printf("%4x:" DISASM_SPACER, pc); } count = disassemble_insn(&ctx, image, len, pc); diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index c9e171082cf6..9a149c67aa5d 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -172,7 +172,7 @@ int map_parse_fds(int *argc, char ***argv, int **fds); int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len); struct bpf_prog_linfo; -#ifdef HAVE_LIBBFD_SUPPORT +#if defined(HAVE_LLVM_SUPPORT) || defined(HAVE_LIBBFD_SUPPORT) int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, const char *arch, const char *disassembler_options, const struct btf *btf, @@ -193,7 +193,7 @@ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, } static inline int disasm_init(void) { - p_err("No libbfd support"); + p_err("No JIT disassembly support"); return -1; } #endif From patchwork Thu Oct 20 12:37:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13013398 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45E21C43217 for ; Thu, 20 Oct 2022 12:37:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230160AbiJTMha (ORCPT ); Thu, 20 Oct 2022 08:37:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230137AbiJTMh3 (ORCPT ); Thu, 20 Oct 2022 08:37:29 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEE93275DE for ; Thu, 20 Oct 2022 05:37:26 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id n12so33998287wrp.10 for ; Thu, 20 Oct 2022 05:37:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7Udp2ZXgLhv3fTpUhsQtFyg4gt4TLG5RtdOt3QsPbyo=; b=VTPDd3OBd9A23Poq3SfSIGO1+eOOnEAJbv/cjPvPrubxV29K4Pqa3E1ckuANdhKaxE YfzdWMR0gSkNqWLGaC6d9UpeSFk37ZpuYTPeQtNbyJj3Igde36a+AnMh4821niuX5vsa yhEBeWM10bM3NZ1OWQaJi44s1IgptiOCX+EI/6Q0D3tqCWMKvYQlfa+nZui6mt114ZUi fWncGpq8muRz75t80rhrxaUfaaeQTzozGuzIqKJseUyVwBO4Np4X5epY9ShVSZD76GEj 6abL8aGY7/tkkYcPUPkb1+AEInHZpr5TAopxa1zjIariBgw91CGIh3ySiqMonxjlz15s vnrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7Udp2ZXgLhv3fTpUhsQtFyg4gt4TLG5RtdOt3QsPbyo=; b=MxI/fbPGFxbECzEdWhaDljEDy4/VpgvWlubXNT4JHNmuyqeLa/Nb/Ft3kofZ13VSrn CsnEA3ohu2dsiUtoawJxCCVIqNSfNI6u4f6uQaiCfnjyb0829rcbaCxRO7coTb+ZZNU4 A7rc0FOhvHJo77RlIB13o88IrWBoVf5BCOsZSrQn3NwDafN7WO8XV16ReOQ4tOtJatc4 ZZlKaSa3BuIP6i7iUoug7A0WFZ8QnMooAFEx+v3A+0ge425xUCoOl2XZ5vmeOdf72V20 8lPRiD1v0cDVV9BixD3zs12LpNWjHvJzI5Hm4oXredxi5/c4U8d54wo1hBHA88C+UuhF MbRA== X-Gm-Message-State: ACrzQf160s9NyMJs/tJN47QfPZVLU2Abfo/oMg/T73QrVr5THCvFDjVW 4niHLnYmuu3Q3ozP2bxcyz/LvQ== X-Google-Smtp-Source: AMsMyM7yxJq5workCdvyA7gakYWt3KQjnlDILDRE+RNnI1CJ7Ked0UELoKKNnoNcfbfBlsAHt8umfg== X-Received: by 2002:a05:6000:981:b0:22e:6b59:fe52 with SMTP id by1-20020a056000098100b0022e6b59fe52mr7998121wrb.188.1666269445228; Thu, 20 Oct 2022 05:37:25 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id h10-20020a5d504a000000b0022a9246c853sm16329581wrt.41.2022.10.20.05.37.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Oct 2022 05:37:24 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet , Song Liu Subject: [PATCH bpf-next v3 7/8] bpftool: Support setting alternative arch for JIT disasm with LLVM Date: Thu, 20 Oct 2022 13:37:03 +0100 Message-Id: <20221020123704.91203-8-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221020123704.91203-1-quentin@isovalent.com> References: <20221020123704.91203-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net For offloaded BPF programs, instead of failing to create the LLVM disassembler without even looking for a triple at all, do run the function that attempts to retrieve a valid architecture name for the device. It will still fail for the LLVM disassembler, because currently we have no valid triple to return (NFP disassembly is not supported by LLVM). But failing in that function is more logical than to assume in jit_disasm.c that passing an "arch" name is simply not supported. Suggested-by: Song Liu Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/common.c | 10 ++++++---- tools/bpf/bpftool/jit_disasm.c | 15 +++++++-------- tools/bpf/bpftool/main.h | 3 +-- tools/bpf/bpftool/prog.c | 6 ++---- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index 4c2e909a2d67..e4d33bc8bbbf 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -627,12 +627,11 @@ static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name) } const char * -ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, - const char **opt) +ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt) { + __maybe_unused int device_id; char devname[IF_NAMESIZE]; int vendor_id; - int device_id; if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) { p_err("Can't get net device name for ifindex %d: %s", ifindex, @@ -647,6 +646,7 @@ ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, } switch (vendor_id) { +#ifdef HAVE_LIBBFD_SUPPORT case 0x19ee: device_id = read_sysfs_netdev_hex_int(devname, "device"); if (device_id != 0x4000 && @@ -655,8 +655,10 @@ ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch"); *opt = "ctx4"; return "NFP-6xxx"; +#endif /* HAVE_LIBBFD_SUPPORT */ + /* No NFP support in LLVM, we have no valid triple to return. */ default: - p_err("Can't get bfd arch name for device vendor id 0x%04x", + p_err("Can't get arch name for device vendor id 0x%04x", vendor_id); return NULL; } diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c index c28b21f90cb9..58a5017034a2 100644 --- a/tools/bpf/bpftool/jit_disasm.c +++ b/tools/bpf/bpftool/jit_disasm.c @@ -84,12 +84,10 @@ init_context(disasm_ctx_t *ctx, const char *arch, { char *triple; - if (arch) { - p_err("Architecture %s not supported", arch); - return -1; - } - - triple = LLVMGetDefaultTargetTriple(); + if (arch) + triple = LLVMNormalizeTargetTriple(arch); + else + triple = LLVMGetDefaultTargetTriple(); if (!triple) { p_err("Failed to retrieve triple"); return -1; @@ -128,8 +126,9 @@ disassemble_insn(disasm_ctx_t *ctx, unsigned char *image, ssize_t len, int pc) int disasm_init(void) { - LLVMInitializeNativeTarget(); - LLVMInitializeNativeDisassembler(); + LLVMInitializeAllTargetInfos(); + LLVMInitializeAllTargetMCs(); + LLVMInitializeAllDisassemblers(); return 0; } #endif /* HAVE_LLVM_SUPPORT */ diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index 9a149c67aa5d..467d8472df0c 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -203,8 +203,7 @@ void print_hex_data_json(uint8_t *data, size_t len); unsigned int get_page_size(void); unsigned int get_possible_cpus(void); const char * -ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, - const char **opt); +ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt); struct btf_dumper { const struct btf *btf; diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index 345dca656a34..aa041ae8ec32 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -764,10 +764,8 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, const char *name = NULL; if (info->ifindex) { - name = ifindex_to_bfd_params(info->ifindex, - info->netns_dev, - info->netns_ino, - &disasm_opt); + name = ifindex_to_arch(info->ifindex, info->netns_dev, + info->netns_ino, &disasm_opt); if (!name) goto exit_free; } From patchwork Thu Oct 20 12:37:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13013399 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D3E8C433FE for ; Thu, 20 Oct 2022 12:37:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229988AbiJTMhb (ORCPT ); Thu, 20 Oct 2022 08:37:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230175AbiJTMha (ORCPT ); Thu, 20 Oct 2022 08:37:30 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5803013D19 for ; Thu, 20 Oct 2022 05:37:28 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id f11so34185467wrm.6 for ; Thu, 20 Oct 2022 05:37:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GV9CmvCIlsYdLq/sGCIYcCoDOmQGMNCcQVEpg6K0tb0=; b=O5neuHPcsnueCL+CJvWgrMUDzEkdPs/BexB0eGicHIF5o7qLLty4aDC5qcVDKVzWHd dH/UUjZokpwtsL3F0Owo0e/LZIyaQS9t4ZHh+EdfhIOEzyi/gMYRRHeF+u2xGO74keRQ kNBB7y5Im3tARFezmBc+A6N12pze3Atr/olE7BzSq8HrKNl9iETPkscpgChNeZgfLx9e U0oYzgsnazWT5RzjbhR/1CgI1jEIMUFvsdDuWD+WAUMhrTHVMLx8jLkLFIkOsTe9Dk5S boFTD2AanDgKSLuH1X1gt+B0E2D8bPdNKOPgeShBBND/0ui6kX6y9NaKVrKsHfcS5H2h ARQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GV9CmvCIlsYdLq/sGCIYcCoDOmQGMNCcQVEpg6K0tb0=; b=QoNWZqWEfeJRMhTS1ueM/hBag5jPCl0FWn9Q8v65/ZkUmIv5B2gnnJr96pNonPWYO4 ITbwEbaLZXvJdaDrbJR67ViaqZIHVy7fB87UCwzoAi5EMWfig5UYSNMmqRsl/4yRW/dy 4FAZmqXz32WCGnWiuAlvc0uG3Xp7ieVQFLNZVKE4xhwcYnWACE1LR3tCqAcRnMvI8OrJ 9sKc2afi7hihTvnFryaImwpcmurHVYvkutQ/bQ4z35PBrh8u9ymO1PyLE7ev4PISJsHg ZAyD6nqubumwSV9Sw+1ad+vt8KoPwjodzxLu4oA5VZARbG76s+KiACq1IOck7hQRJ9M4 evaw== X-Gm-Message-State: ACrzQf0b7Tfji+xC/j1xN5YwLsp01+CXULmNRgXoRjqj5JVGL2q20TRo 5uepop6XWgqGXw25+gOqA8311w== X-Google-Smtp-Source: AMsMyM4FYNqGWyQZEiYyCaZsKNFJKOqpB6I1eKxDF0dZEZg1JiMXVp7XwyRHN3NIbuS1/Rsfix87Aw== X-Received: by 2002:adf:e549:0:b0:230:6d12:fc84 with SMTP id z9-20020adfe549000000b002306d12fc84mr8454773wrm.64.1666269446453; Thu, 20 Oct 2022 05:37:26 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id h10-20020a5d504a000000b0022a9246c853sm16329581wrt.41.2022.10.20.05.37.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Oct 2022 05:37:26 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, =?utf-8?q?Niklas_S=C3=B6derlund?= , Simon Horman , Quentin Monnet Subject: [PATCH bpf-next v3 8/8] bpftool: Add llvm feature to "bpftool version" Date: Thu, 20 Oct 2022 13:37:04 +0100 Message-Id: <20221020123704.91203-9-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221020123704.91203-1-quentin@isovalent.com> References: <20221020123704.91203-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Similarly to "libbfd", add a "llvm" feature to the output of command "bpftool version" to indicate that LLVM is used for disassembling JIT-ed programs. This feature is mutually exclusive with "libbfd". Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Yonghong Song --- Note: There's a conflict on this change with the patch at https://lore.kernel.org/bpf/20221020100332.69563-1-quentin@isovalent.com/ Supposiing both are accepted, I will of course rebase one or the other, accordingly. --- tools/bpf/bpftool/Documentation/common_options.rst | 8 ++++---- tools/bpf/bpftool/main.c | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/bpf/bpftool/Documentation/common_options.rst b/tools/bpf/bpftool/Documentation/common_options.rst index 4107a586b68b..05350a1aadf9 100644 --- a/tools/bpf/bpftool/Documentation/common_options.rst +++ b/tools/bpf/bpftool/Documentation/common_options.rst @@ -7,10 +7,10 @@ Print bpftool's version number (similar to **bpftool version**), the number of the libbpf version in use, and optional features that were included when bpftool was compiled. Optional features include linking - against libbfd to provide the disassembler for JIT-ted programs - (**bpftool prog dump jited**) and usage of BPF skeletons (some - features like **bpftool prog profile** or showing pids associated to - BPF objects may rely on it). + against LLVM or libbfd to provide the disassembler for JIT-ted + programs (**bpftool prog dump jited**) and usage of BPF skeletons + (some features like **bpftool prog profile** or showing pids + associated to BPF objects may rely on it). -j, --json Generate JSON output. For commands that cannot produce JSON, this diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c index ccd7457f92bf..7e06ca2c5d42 100644 --- a/tools/bpf/bpftool/main.c +++ b/tools/bpf/bpftool/main.c @@ -89,6 +89,11 @@ static int do_version(int argc, char **argv) #else const bool has_libbfd = false; #endif +#ifdef HAVE_LLVM_SUPPORT + const bool has_llvm = true; +#else + const bool has_llvm = false; +#endif #ifdef BPFTOOL_WITHOUT_SKELETONS const bool has_skeletons = false; #else @@ -112,6 +117,7 @@ static int do_version(int argc, char **argv) jsonw_name(json_wtr, "features"); jsonw_start_object(json_wtr); /* features */ jsonw_bool_field(json_wtr, "libbfd", has_libbfd); + jsonw_bool_field(json_wtr, "llvm", has_llvm); jsonw_bool_field(json_wtr, "libbpf_strict", !legacy_libbpf); jsonw_bool_field(json_wtr, "skeletons", has_skeletons); jsonw_end_object(json_wtr); /* features */ @@ -132,6 +138,10 @@ static int do_version(int argc, char **argv) printf(" libbfd"); nb_features++; } + if (has_llvm) { + printf(" llvm"); + nb_features++; + } if (!legacy_libbpf) { printf("%s libbpf_strict", nb_features++ ? "," : ""); nb_features++;