From patchwork Tue Oct 25 15:03:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13019418 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 A8A8FC38A2D for ; Tue, 25 Oct 2022 15:03:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232439AbiJYPDp (ORCPT ); Tue, 25 Oct 2022 11:03:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232209AbiJYPDm (ORCPT ); Tue, 25 Oct 2022 11:03:42 -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 7BC581B2BBD for ; Tue, 25 Oct 2022 08:03:41 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id j15so11051230wrq.3 for ; Tue, 25 Oct 2022 08:03:41 -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=UM6m8ABDHy9/yDjET8mC473qdbIttwuMCtSmPhs3VzM=; b=0OodaKVssFUxxQQTJWxrIB1mfo1iaGuCAnIZU53ibrmEO7luzo/RYwlC3ueCwEP0Qs OuJ1y7nxVJE1cXQ8992JsZNsQ24ESK+MvwIZhpjtJOdbm/rQHHZZkUTQk/1kqFoGliGw fuuEumnx3XJb4Q2I764PDzEd/mdQw4vi6phpbodNbqQjjuQC8ab0LE6kiXrVj/2BRBSa 4Y0PDG0gIPXKUvBk+je9mH7Ts6EPtitY75lPOy0uIj8Y+4jqF4DbJF4TP0UtpPxqsxfo SuGbE4XtSp8SF5IvAhkNWzNrefR/rZc6xkZMpuD3nGg3pd4+9OoEBByxoHdVn99BKORh e8Aw== 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=UM6m8ABDHy9/yDjET8mC473qdbIttwuMCtSmPhs3VzM=; b=X2kiKwz4YG1SFP30dQWdIMAxebwBrgp97g5qrZQWiEP1aFkoZEJWYLie5vQ3HJQpb3 oxLMQyJVPxAQb4Siex8TTfJ9a3+q4oyR3YrdU+UU462zLuicTyRoQAt7lQQV+QfifaYP xsy4BeBuYkwXXDdS3JyDx+J3RPdwSr7zPNMnU3yjatxtNtLPTLhxzm0e+HFYerX9o7c4 egMCTvuCUtGZ8N4yo5chqjdmOikyEr3EzzPTvdtxEd75BoEW4/am2vG+vUWr+VM+9JTx IBqup4zatYTXb1jcjrmihBl17Wd4NOzzwM5OqPZRB6o+abUU8oi8+T8flVJElZDF9SDW IR0Q== X-Gm-Message-State: ACrzQf0Fmu8K4GmV9VwWvG8joJ04Y+XGum39VvazQkObVFi8nBWTRndB eu02cNixVCNaber6dpFUFdISDw== X-Google-Smtp-Source: AMsMyM6BSnj8J0VN8ymMU0fvo1NJ5OyOpcdx8TKPctiZIMw+i7UgouiIj+rEh/eeOc55+K2wFDYl3w== X-Received: by 2002:a05:6000:178e:b0:22e:6d9f:1592 with SMTP id e14-20020a056000178e00b0022e6d9f1592mr26137626wrg.6.1666710219688; Tue, 25 Oct 2022 08:03:39 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id i7-20020adff307000000b0023659925b2asm2724182wro.51.2022.10.25.08.03.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 08:03:39 -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 v4 1/8] bpftool: Define _GNU_SOURCE only once Date: Tue, 25 Oct 2022 16:03:22 +0100 Message-Id: <20221025150329.97371-2-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221025150329.97371-1-quentin@isovalent.com> References: <20221025150329.97371-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 10ec29cb4598..dbf5489b8fde 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 Tue Oct 25 15:03:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13019419 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 4FDD1C04A95 for ; Tue, 25 Oct 2022 15:03:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232209AbiJYPDq (ORCPT ); Tue, 25 Oct 2022 11:03:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232035AbiJYPDo (ORCPT ); Tue, 25 Oct 2022 11:03:44 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B70B1B8654 for ; Tue, 25 Oct 2022 08:03:42 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id z14so8057130wrn.7 for ; Tue, 25 Oct 2022 08:03:42 -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=IQCWAMd9K0EQTELzO61ETzXVpVx6gA2DYp10qkgup5k=; b=lfiEy6AqGlArcqoTLlW8+ovVQo8zHZNMSjdqeGJCjkhLh3CWJruqPlut+cDzfs8b2u hPXo9TazmPgY5eQv2XNxk//CIAIbx3lRUBbtarfc+WxXjqZe2zLQY5FKYhCnS4OMBcsE UdKp6PpsyM3FZliPu0d7L542T/WvDUwQQ8LnzmOxFujv8q84K4Gp8UtmZoQ3jkUEzflB GUxaypn+e0cexwe9og7w32D0mqcyPeKfOx1li8Gj7wlYbzKrUTycsYmye+BndyNx8alB slh2NCNvxYs7MHUrIm+xBcuOn6MPT/RoB5OaIC99cRkph05LGtFawrAwqhA1Z0/a7cYR 3fPg== 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=IQCWAMd9K0EQTELzO61ETzXVpVx6gA2DYp10qkgup5k=; b=YWIdZ/axIDPCCRa74qCEaE3DlBaiB8x7ATZ/xaztou/u4UIQEWkvhEoIrw0B9TnEof 7nsOucPsqqkBSyb9HAl7DcrALAF7gk/H92b3Pm6UbInu4603+AIJmtrjcpYKFZbYPsK1 gfqMg5mzhQG7+xSHRgBW/Z4J6vlunDmExVCXPbmkDgloDhOftS4IDa+dhMgu4Hri9eQk QR5u7ctP8cTyQwY/t72R0yRJIx3ySkWH1XE/rTRlq4l8KluC7QAmBdKfh3jscqIkkh7M Caj8uMO8LCFnJm7faDGq3DB7Ni/beflVxHEXq5oCFFVjXht6wvTBzFkmVWv5qq+P/vV8 czWA== X-Gm-Message-State: ACrzQf2vU670nm9DT70AU6Z+iVnS+O31YMHmVsya3jly7qVWEhGE/alq /fctOzIjmqH1G0wkjHtsYxIn7g== X-Google-Smtp-Source: AMsMyM5x9m7ErifPcXBdD5y06U7f5S7uOQw/pS2gED9cnxZsREbQVrzzLJ9AyAhSsXW8TqUjCXYGyw== X-Received: by 2002:a5d:6f1e:0:b0:236:63f8:d03d with SMTP id ay30-20020a5d6f1e000000b0023663f8d03dmr10908472wrb.646.1666710220785; Tue, 25 Oct 2022 08:03:40 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id i7-20020adff307000000b0023659925b2asm2724182wro.51.2022.10.25.08.03.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 08:03:40 -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 v4 2/8] bpftool: Remove asserts from JIT disassembler Date: Tue, 25 Oct 2022 16:03:23 +0100 Message-Id: <20221025150329.97371-3-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221025150329.97371-1-quentin@isovalent.com> References: <20221025150329.97371-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 dbf5489b8fde..93dfb89b85e3 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 Tue Oct 25 15:03:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13019420 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 BD352ECDFA1 for ; Tue, 25 Oct 2022 15:03:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232035AbiJYPDq (ORCPT ); Tue, 25 Oct 2022 11:03:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232388AbiJYPDp (ORCPT ); Tue, 25 Oct 2022 11:03:45 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A9D81AFA9A for ; Tue, 25 Oct 2022 08:03:43 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id i5-20020a1c3b05000000b003cf47dcd316so467192wma.4 for ; Tue, 25 Oct 2022 08:03:43 -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=ysyODqMpzYEB5xwLCraBYcLjXoKAXNE5QsQ/tjIJn6kZmsLNVxGAIRv3chsx4EdD7K qTVCbsunuskACDbBPj10YXeLBcFYteTC7FVFuyvfh4ZbF3h0Y/BJouyYp79xuwNgpGGC HTEP5kUOtZeNw1sW35hrV1q5YSGzWAvJvM8ONnY4C3XXQiLAEhCROAetx+Zw1i4q4Ku4 zV+yOsTfTXfUmUy0lARWnapmSz09zh/K7H18Kk+zaqF1ncably4Ik6zvk7XDURRONN3a oHImqFndlEx9z9tV/aodPHSHueBl3vlhQwbV5gFLjsgOIhUoE6l+sQ26nOZD1ny7VPpy qrvQ== 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=IEV41BDZ8zyAs0jEZEdfkWVguvQPhS+UM6OoONGO5iU9Q07yjkAa2n9pXILfx71kOk hzkzpQvOe+DJye8hlFUcG+p91maG4ozT3EYZzcIZbsyv6VMBv3UFDtICQtC96K2+Opdd BA2P3jlvxLpVuoiiMUB/qmzq2lyKFexprStiHsNoQJw9/8trvZaNPb2UHWyaliJApP1+ K7HJNRtoAa0OErN1CpscjiGLeyUXfi1hesSLQyyrMe1qjPpa+ECjxNoJqEQe/6+7vmL0 LaFaUe5JqJpWt24neoD+RMVpKQKs28CxZQG5Lnz83t+7fU4A6XLocOaX1TQT7lkOF8ix RTTQ== X-Gm-Message-State: ACrzQf2KZHjnKILEwB64m/M6nJm7ygdJfyUPX5ZzTNzvZyJ+d3aX01rv 4Ae8sZ3Ib7N4KuQpdeNvKmQrRd3NAzTjOw== X-Google-Smtp-Source: AMsMyM7px89BPjzAwS8P2C6U3KErEMP7aWbVDABGvvdknm/nDI2FiWTSTt/aKUBUL0MSwnbLFfGlTA== X-Received: by 2002:a05:600c:1990:b0:3c9:a5e8:addb with SMTP id t16-20020a05600c199000b003c9a5e8addbmr11564995wmq.140.1666710221798; Tue, 25 Oct 2022 08:03:41 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id i7-20020adff307000000b0023659925b2asm2724182wro.51.2022.10.25.08.03.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 08:03:41 -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 v4 3/8] bpftool: Split FEATURE_TESTS/FEATURE_DISPLAY definitions in Makefile Date: Tue, 25 Oct 2022 16:03:24 +0100 Message-Id: <20221025150329.97371-4-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221025150329.97371-1-quentin@isovalent.com> References: <20221025150329.97371-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 Tue Oct 25 15:03:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13019421 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 52107FA3740 for ; Tue, 25 Oct 2022 15:03:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232377AbiJYPDr (ORCPT ); Tue, 25 Oct 2022 11:03:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232445AbiJYPDp (ORCPT ); Tue, 25 Oct 2022 11:03:45 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3133D1B2BBD for ; Tue, 25 Oct 2022 08:03:44 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id bk15so21556462wrb.13 for ; Tue, 25 Oct 2022 08:03:44 -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=JPMZYnmmHaa8KqJLQJhzJhdNtIxq9NnnISGOV36btcqqtJJb4OcKbxP2byfbhljX8n T/ApZHjurfCcw6zKHFtJUly1KJIpSWRfOpGufkXUEJv6/C9rFBafphGPOHy3OxfOjBSF bMdfFq9paVvMO5sL93MjQFwjLuEi01Rl3eLT5F8p0FqXbRP2eoVeaeYuj15ObsZWxxU8 OxA8ofwvSs6xgeHKxNhTYbZkIwaNwWbTB7GWNqLwCQci2FlI5eAeJS1v9DJ5WEBM58he Rz9aH6QIqTwSvlnjqBrhdbjlLEIW/noG9NAaI8eQlt3V+QsjxS++4H/AQxRRUGczZ/1l vpcQ== 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=zv65EYodyJVnGGAB7FBypEqjsa2Kj6WNjl3ajjZQxNE0U+LD8RFAssmqrR1XTRMZqr IIJI3PuTxZL906YWQXIOjhxUPV1z6y93NrGugOBMWNIVnSYVvXOl6fQymKlqQPimByPx ueCdAZdwiU/E3zxkwEunC5BUZ+5CGDiPW3WDNObzqm1efyrkNkWnNuYkPWRCtBEYFhl6 7ajdkkd6F1mwdMdXDUYft0zF6DLi07HyXqR6j+8908PlGJBFl4LaxX6VCoR8a9tjfmvC W6OWCdbD/LC+v4Lkfw5VoZJBxkcqiPY6y30FIJx3tJpvfVyy/8jL+r7h1jqV9gWRfD63 8AkQ== X-Gm-Message-State: ACrzQf1T/HCMHbnYfvZ/BPE0GR2XagTTCcDgwnl4QUjJ7vFjW4SR24XY 13XQ74+f2lNF3u6pelx15ndDSA== X-Google-Smtp-Source: AMsMyM7pdt3lyJb2qxt+ZfuICRvOoxmvUe7cmmXS60HUGOSZcs0Iqm7jbvZAIuNpL2gCNkIQQtHVQw== X-Received: by 2002:a05:6000:547:b0:218:5f6a:f5db with SMTP id b7-20020a056000054700b002185f6af5dbmr25954025wrf.480.1666710222756; Tue, 25 Oct 2022 08:03:42 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id i7-20020adff307000000b0023659925b2asm2724182wro.51.2022.10.25.08.03.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 08:03:42 -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 v4 4/8] bpftool: Group libbfd defs in Makefile, only pass them if we use libbfd Date: Tue, 25 Oct 2022 16:03:25 +0100 Message-Id: <20221025150329.97371-5-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221025150329.97371-1-quentin@isovalent.com> References: <20221025150329.97371-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 Tue Oct 25 15:03:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13019422 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 D765CC04A95 for ; Tue, 25 Oct 2022 15:03:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232373AbiJYPDs (ORCPT ); Tue, 25 Oct 2022 11:03:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232269AbiJYPDr (ORCPT ); Tue, 25 Oct 2022 11:03:47 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39F0D1B7F14 for ; Tue, 25 Oct 2022 08:03:45 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id h9so10483156wrt.0 for ; Tue, 25 Oct 2022 08:03:45 -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=c+XI2AGNsBI91Dq/ptTtzRJmcxOs5DiaB451uOmcBz1PAMHDZd/HKGRmpcmQVFHz3R gvLzGSnNKeUaqPz5I3FJeUqx5rKTxl4b00MfNUXYt1wG9uXswipOeLVTks2QWVSlaMKT xJlx0NROArI1lPAtyz7TAzeGQtYtZmsDEfVPSaxpgzmo29KmAzWhRIfEUV11k8KFWWi6 yt1QNjH/bPGNFT5Q8F7jz2EIopgoBIR+91gahYh8K6GTSqt12sotsbNsrQedbIwT/C8M wkMLk1jaL3+YRJVaVAz9XjgxOOtxXz+kocu/vRv7z1Nsc8pg3APWZlfikSwCeH0EejWU tGDw== 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=jhh+Ndc2Dary3nuXVBzJmtlQbidQc8gPVejvT4tUYz4etfVFDQOY7w2R2U3L98FUSn vE1ydp0d58F7bWJZ+egWiKVUzOfUFtT8vktADX/mCuWo84dkpATUDZyI0wNZSSDlHIO7 hLMzPYmBjob/jnkoO9PfaAVPybBfr1/PnYCQ+uyZlkqmYRNqV/mWNiRt4gwf6dQsQVBp beLt/jZdwLGLct0jf/mDqFKs04yW5IfkMMF0qCouWto8gTklZycoVanN8BUYrsXDqxjP F4TzNGtsz7o29EtQjS6BsVxDjtH3Yr5VrsQQhkQfMu4Nacvq9hz0gcRhL38BA18E+Vjg aQDQ== X-Gm-Message-State: ACrzQf2aOOLuMEAmCSVqmiHkkXC8DaLAnoiu1JBpsTIlE8bn/CgK52Qq 3uu0z0rUCifKprJq//VkizuNIw== X-Google-Smtp-Source: AMsMyM6itCkyD29r7lO30+tYYGQUm57Ug0q2t1kvKvWBznULxB5cbpHZ8aO5n6vXsnUYvDj+ddPvHA== X-Received: by 2002:a05:6000:1e03:b0:22e:3bf2:4685 with SMTP id bj3-20020a0560001e0300b0022e3bf24685mr25580064wrb.82.1666710223550; Tue, 25 Oct 2022 08:03:43 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id i7-20020adff307000000b0023659925b2asm2724182wro.51.2022.10.25.08.03.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 08:03:43 -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 v4 5/8] bpftool: Refactor disassembler for JIT-ed programs Date: Tue, 25 Oct 2022 16:03:26 +0100 Message-Id: <20221025150329.97371-6-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221025150329.97371-1-quentin@isovalent.com> References: <20221025150329.97371-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 Tue Oct 25 15:03:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13019423 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 16847FA3741 for ; Tue, 25 Oct 2022 15:03:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232388AbiJYPDs (ORCPT ); Tue, 25 Oct 2022 11:03:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232373AbiJYPDr (ORCPT ); Tue, 25 Oct 2022 11:03:47 -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 4B04F1B8659 for ; Tue, 25 Oct 2022 08:03:45 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id o4so13351005wrq.6 for ; Tue, 25 Oct 2022 08:03:45 -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=25h85Sw+QkgnbhIbE03FHU5WGRfrdOCNQqnfmZUlSO3rNT+h/J2vJhNkwQDLtPmVT7 mVKUJGMVr/tUEzlXzU7kLhkC/QV8cA7MjXFmePfHqeuR0X8qLjQ56SHm7641ui51BY/l 8cnA3qUhPLjetlxEOosOhCrz61B1vA8yYJHzcdpMIt5qC3x8PRme16w4Gy/IxMf4zBN1 9g7yrSJXJuC1UbbIty+i4FIohxBq+4O9XG4n0X4dk7S5+4i9TC1o64JE3R8HyuuxzCg2 Mz5yt75ydB0kAMIVm1lyTSZRYBE5/k7kA5Mlk6VgtYzcwNP3en8XgKhP0f06J5Bdix5X 8gQA== 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=FqXnaPQgxjTYsJ58kr2pA48xN4VfdbEulWdkP/WeVpxAEh/v2NTxB61zd8Vrh7o7uP WZf+pcxmUesImVCaxTzi3DYML9zpC4t++QIVYD8F8cWndjk2R/FM7DBRO3H+Bav84KQj nh54k6aoP9q0rxrqUkI2UlAYMQLZQWr/7J4a6XTtLV1HvP7WExCJZE5EO+QGMHVxQcNn VJojG/kxZHmgG/RQs/HDNJJ/mIaYFQUQTEVVuab27C/Tqd/ZLaDao/FeImrZArsUdtA/ LQ7Ch+ZZBmroQD/q+ozM0CtpiWp3JcDMW0Sj8RijrHQmLzXeLegZxBLEDWkL2RIC3oMW OfFw== X-Gm-Message-State: ACrzQf3Xt6+tvqr+Vj3TQMc1T13yoLGPTjSQfHWQqWFUZXtUYRIxp17R sjkZt21srofQebhMfKya+r1wzW3nlKNOxg== X-Google-Smtp-Source: AMsMyM4YNW6VJBYcuY5J+Xsr6IfEd5OsLzy7v5jVKHG2uAkQsG36ySa05JfFU60aGth6M3JyQLM7sw== X-Received: by 2002:a5d:4711:0:b0:236:48b6:cb89 with SMTP id y17-20020a5d4711000000b0023648b6cb89mr18290844wrq.246.1666710224791; Tue, 25 Oct 2022 08:03:44 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id i7-20020adff307000000b0023659925b2asm2724182wro.51.2022.10.25.08.03.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 08:03:44 -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 v4 6/8] bpftool: Add LLVM as default library for disassembling JIT-ed programs Date: Tue, 25 Oct 2022 16:03:27 +0100 Message-Id: <20221025150329.97371-7-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221025150329.97371-1-quentin@isovalent.com> References: <20221025150329.97371-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 Tue Oct 25 15:03:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13019424 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 D2655C38A2D for ; Tue, 25 Oct 2022 15:03:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230305AbiJYPDt (ORCPT ); Tue, 25 Oct 2022 11:03:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232460AbiJYPDs (ORCPT ); Tue, 25 Oct 2022 11:03:48 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24CCD1B866F for ; Tue, 25 Oct 2022 08:03:47 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id v1so21564655wrt.11 for ; Tue, 25 Oct 2022 08:03:47 -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=hbFKA7BbKkVTCiDA2jNGQBqtk+RU53oHgNWLeKHi3B8=; b=pn+1Yd8jAgWYNgBymUdEjCV9xRIxSsgnQtoAwk0n6MYHSAVQrJoHVOm0g65Qg+RQNh tWOoylsyCrMrIfVoDTbGNxHHCB8oZzrcUtlfeLCrJTBAoDn9tvF8w6lViAWjzqVFz1qE YeVvUVRMSyTpJbIWEEjMbZmxIqlXCTNF6X9+lKqJsKjG2xUaVE4We52SSzPvAd0Q+G2N Wm16Rq777twjeOyteuODFr6x8tiN8hKYyAnyxqrOZ2Vvwk7ndxF4irZlWhmLqd0hifhq yE6LQh7iikECynRGmx189SR5s0GvtSoYGkgmjFWF/BVQ7apbQBWM4YRrvCfd82Mq56K5 mUwQ== 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=hbFKA7BbKkVTCiDA2jNGQBqtk+RU53oHgNWLeKHi3B8=; b=gEUy7A2Cai+PISQhIS8ivWxvYoEhmPHbuK/d61fpUo22oF0rvQf9piqzqLQ4KtAn7W N4Aqs4OI//lscAVHezXVpIEuRYq5UBUInh8SYImq03BEtfFJTtl/G5dYSj3aZUZnJ6GB oxIK1YHz/dn+nytBjIoQPHxckxJKe2dtBWkV1+6IaeHb2DXYghwVm724qr6X6OGHcU3W ppN59etI8gIe9lm8O0MKY0nsIH5ao2YfPEPub98nZUqK5R3fJu9b9QrjvBhxNG5wHEHH ZiPSxBBdaNt3doG6+vGLHMl4pkE8xHj+x5w8jERm2cNh+ZIhSkHNwj2Ss2n4wJpi2Gis GI/A== X-Gm-Message-State: ACrzQf0w0lYu/VNywhI0K58CoAP5Dg+ZHu2rykRKZIYyCCwa5BYhIi3d 8b1mSNxXXlOavZJ2TyQWR+8KXA== X-Google-Smtp-Source: AMsMyM4Az2sY8hpM0Qh8Ju8Gl3lFFjDmJBm2YWRnffWeCd4nudwcGt+QR7v4R/VE0wJtpxW1hselGQ== X-Received: by 2002:adf:f84c:0:b0:236:6e52:504 with SMTP id d12-20020adff84c000000b002366e520504mr8374851wrq.564.1666710225692; Tue, 25 Oct 2022 08:03:45 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id i7-20020adff307000000b0023659925b2asm2724182wro.51.2022.10.25.08.03.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 08:03:45 -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 v4 7/8] bpftool: Support setting alternative arch for JIT disasm with LLVM Date: Tue, 25 Oct 2022 16:03:28 +0100 Message-Id: <20221025150329.97371-8-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221025150329.97371-1-quentin@isovalent.com> References: <20221025150329.97371-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 93dfb89b85e3..a858b907da16 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 Tue Oct 25 15:03:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13019425 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 82B92ECDFA1 for ; Tue, 25 Oct 2022 15:03:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232269AbiJYPDu (ORCPT ); Tue, 25 Oct 2022 11:03:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232537AbiJYPDt (ORCPT ); Tue, 25 Oct 2022 11:03:49 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 928261B8654 for ; Tue, 25 Oct 2022 08:03:48 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id l14so14334997wrw.2 for ; Tue, 25 Oct 2022 08:03:48 -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=mHQ6Nh6S2X3LiORSKMcrUXr81sCAtwRLflLk9YztkJQ=; b=TuupZUhN8/Rpk5xiI5AzNBI+sZtlaB8cR22HmienOPbLorsITNwDnUsCCVGTR18GEn 3vIwsUd/Y1XK0TxxMSdVr6L0Rom46idnTqbNRX03VhKIaen1Uvu4gI2c9xIrw5Gf3fQq nGJlokYfza99XdgseODpVTNMGlbLq37edcShqI3TkXBOj6hddAm3q4p+8Bv3WEC09mAn j/wEXYRGfgtJcvcjOOk7l+lx5ddixfafLdQc1fuCiCuQUrm7tDO+zWFY6WQb0GWzoX8M Zt5DiLg8bxIrnGuivw9feB4Hf3KFN41zGP45n+FSv5KmQjzHH+1wvjgApFvE7tk1iX+a XAqQ== 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=mHQ6Nh6S2X3LiORSKMcrUXr81sCAtwRLflLk9YztkJQ=; b=Qc6jPsmUZTPNj8tnCGb3FFVKtkwkgFeRcgNKRsLzWBOKiaSGMGfqHFRn9hRFRLkrrq Uw9OUXkqKaE1H5jMHFNFfH11662ezfBoRSkv5gMB/J2j2ElAuHndglgHP7+0I+PuId5J lbEOdGtEOxuoV07TzIIQrG+l8+014f74C+QKxpsnj4niEfhvqMCqMS9yYryX4d0aYrb7 ZFVWx5lGdlCOemDVprq2xIcdOStl3d62OtJk4JdwxKD19pGGRUejKwtGiEQ0j65k64yf yMztDbebxmFwJZuXM6ZZ7bDOsqROMa4ekeL7ekm+RuXPI203+sgDBqgrcsxGY3r3E3lz W2Cw== X-Gm-Message-State: ACrzQf0sXM29Qub4EX7zdqiecwf9WdRUsb1pUHYac8pM7JQf/XbL4w4n GBUPq9SeHC4bQEs/pK+a4NmYPw== X-Google-Smtp-Source: AMsMyM61CxQ99ov9xA+7WhuDYQQv2155C3WLqpLEjo3JB4hC1EAyFxmJhUasZSe9XVBP899cnjZEww== X-Received: by 2002:a05:6000:1a8d:b0:236:4810:9966 with SMTP id f13-20020a0560001a8d00b0023648109966mr18094487wry.366.1666710227068; Tue, 25 Oct 2022 08:03:47 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id i7-20020adff307000000b0023659925b2asm2724182wro.51.2022.10.25.08.03.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 08:03:46 -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 v4 8/8] bpftool: Add llvm feature to "bpftool version" Date: Tue, 25 Oct 2022 16:03:29 +0100 Message-Id: <20221025150329.97371-9-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221025150329.97371-1-quentin@isovalent.com> References: <20221025150329.97371-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 (from Makefile definitions) with "libbfd". Signed-off-by: Quentin Monnet Tested-by: Niklas Söderlund Acked-by: Yonghong Song --- tools/bpf/bpftool/Documentation/common_options.rst | 8 ++++---- tools/bpf/bpftool/main.c | 7 +++++++ 2 files changed, 11 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 b22223df4431..741e50ee0b6c 100644 --- a/tools/bpf/bpftool/main.c +++ b/tools/bpf/bpftool/main.c @@ -119,6 +119,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 @@ -154,6 +159,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_bool_field(json_wtr, "bootstrap", bootstrap); @@ -172,6 +178,7 @@ static int do_version(int argc, char **argv) printf("using libbpf %s\n", libbpf_version_string()); printf("features:"); print_feature("libbfd", has_libbfd, &nb_features); + print_feature("llvm", has_llvm, &nb_features); print_feature("libbpf_strict", !legacy_libbpf, &nb_features); print_feature("skeletons", has_skeletons, &nb_features); print_feature("bootstrap", bootstrap, &nb_features);