From patchwork Sat Oct 23 20:51:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12579831 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD731C433EF for ; Sat, 23 Oct 2021 20:52:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 96B9A60EE3 for ; Sat, 23 Oct 2021 20:52:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231349AbhJWUy2 (ORCPT ); Sat, 23 Oct 2021 16:54:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231213AbhJWUyW (ORCPT ); Sat, 23 Oct 2021 16:54:22 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC8F5C061348 for ; Sat, 23 Oct 2021 13:52:02 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id w15so971023wra.3 for ; Sat, 23 Oct 2021 13:52:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=N1WVOZzTNh/27joMu9FrakmExxycTJXl4iU2zUCXEpI=; b=E7mTjdCnzNu7fTtcv4huP0x9ptCPFwdMYkzVsiL4s8m/XOsFU+EBFV5Coz9i4d1Y2N P95uGIg/MYsv/4B9ObjDV14Xiw6VM/KMt0Wi01LZrjRiwwi9giNxORWzNyebpHfi0jZp gq3wjtPBuhqfn5FPNBsXlUboOeDu9y2mENvijmAP2dKdNuj/xLxhZ14+WzFeYBHRYb50 cWetwT6G3yulvPOwtplL8npUXV/Ifzjay8fEpeZect3AAP9ouAzxN4/sPMN491SIgNqW QZAHadUnofP4WIaXfDh/g44dPPqaXzFviRhMGcVYIWB8J1z6dMLh9Y76Ze4kyVKuVgVS olqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=N1WVOZzTNh/27joMu9FrakmExxycTJXl4iU2zUCXEpI=; b=Ef/OUQi/3fAfFETtYl7gEFwjneyVInozfqJuUaAZAXwzy/jk+9gAA2JCgAq/pSPvQ7 H9lxSUxvYJhP8AlQOOuey8PvhISs/FQLwg9lu56sDKweWp1G34SR8joEZST/n4jVO344 z9U9VSpFG4HjdhZe4OukarwI/833sL5f18HzbxgaMv57LtQncadmzK/J4Hk9buuwqca0 Q8yZhHlC8rfd3iT46wBxgmbyeO+KvpS6aKb+c4CG/+v5TlGHlxijXAyw36EfSc4ZGSIK If8gz676wraWcVjtj9hDvLtqI2GjXdafVJ/aXFy4+ZBn8EtZBrK7nBTZGdVWMSHWZqNp majw== X-Gm-Message-State: AOAM533786cJnvz6+DIGUExQPxfq4hAcEiWraUB4IJSnFRrwF6Ojmrsy scD9t0Ctwx5S9PR28zi8KbGTlw== X-Google-Smtp-Source: ABdhPJwVr4UelzHEpzNVZSRpeOp+gjTZlDra/SRPpPPRcdidctBXC/zlPYvlymlifHu4rlDEQS9/2A== X-Received: by 2002:adf:edd2:: with SMTP id v18mr10309607wro.104.1635022321571; Sat, 23 Oct 2021 13:52:01 -0700 (PDT) Received: from localhost.localdomain ([149.86.74.50]) by smtp.gmail.com with ESMTPSA id u16sm13555398wmc.21.2021.10.23.13.52.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Oct 2021 13:52:01 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet Subject: [PATCH bpf-next v2 1/5] bpftool: Remove Makefile dep. on $(LIBBPF) for $(LIBBPF_INTERNAL_HDRS) Date: Sat, 23 Oct 2021 21:51:50 +0100 Message-Id: <20211023205154.6710-2-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211023205154.6710-1-quentin@isovalent.com> References: <20211023205154.6710-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The dependency is only useful to make sure that the $(LIBBPF_HDRS_DIR) directory is created before we try to install locally the required libbpf internal header. Let's create this directory properly instead. This is in preparation of making $(LIBBPF_INTERNAL_HDRS) a dependency to the bootstrap bpftool version, in which case we want no dependency on $(LIBBPF). Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 098d762e111a..939b0fca5fb9 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile @@ -39,14 +39,14 @@ ifeq ($(BPFTOOL_VERSION),) BPFTOOL_VERSION := $(shell make -rR --no-print-directory -sC ../../.. kernelversion) endif -$(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(LIBBPF_BOOTSTRAP_OUTPUT): +$(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(LIBBPF_BOOTSTRAP_OUTPUT) $(LIBBPF_HDRS_DIR): $(QUIET_MKDIR)mkdir -p $@ $(LIBBPF): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_OUTPUT) $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) \ DESTDIR=$(LIBBPF_DESTDIR) prefix= $(LIBBPF) install_headers -$(LIBBPF_INTERNAL_HDRS): $(LIBBPF_HDRS_DIR)/%.h: $(BPF_DIR)/%.h $(LIBBPF) +$(LIBBPF_INTERNAL_HDRS): $(LIBBPF_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_HDRS_DIR) $(call QUIET_INSTALL, $@) $(Q)install -m 644 -t $(LIBBPF_HDRS_DIR) $< From patchwork Sat Oct 23 20:51:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12579833 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C21CC433F5 for ; Sat, 23 Oct 2021 20:52:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8542F60527 for ; Sat, 23 Oct 2021 20:52:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231259AbhJWUy2 (ORCPT ); Sat, 23 Oct 2021 16:54:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231246AbhJWUy0 (ORCPT ); Sat, 23 Oct 2021 16:54:26 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D83FCC061243 for ; Sat, 23 Oct 2021 13:52:03 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id a20-20020a1c7f14000000b003231d13ee3cso8134613wmd.3 for ; Sat, 23 Oct 2021 13:52:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=q7vZZQ81xPSpmrpG2XPMAeZXt4/fk8crwLDwheVDqVc=; b=rGMy+ylC4M74y8ns7W6VVgb9qZHN8FJ6lpCF6Kj5K3f49FU9zzNXUE2Or0xUq7UXVq ahTHC6YdH3YuzE1cKR9bxk/bMcaPQKGRpPIGYbhMTo5mPb0RuvSnFNqOx6klEgTZvihL iIpEWv7wFF18tq/3RrvS0BuanNA9l4sJ5oSt9rLw75CrX5vKIRpA2nPw/CQqrQfcX+N4 CY2GAZVN1sTP5wT2wefoYiXUqyth5qND4Oq97G8RbwS3lVcnfcVX8xUcvaefFGduzk/t oW3hHaf6OjQru117IPj8JR4TBMQh7sdoV2wDQAQtk8aYdKySx1Ir5+q5qDhyU0aA/5jS 3nRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=q7vZZQ81xPSpmrpG2XPMAeZXt4/fk8crwLDwheVDqVc=; b=T6b3W2O62T9JyPoveA1T1tD2FU5Hy5uzs3orqdpDfv6tqyBTnMAqTrakMkW9K6KQlO LYpN6syCC4T7U2eX6IquFtNlO84Ll8U3SF5EG9AGWFv0T2oQdgvp6fEeZkLwA5vnJprX Ue1jzvNR+C5ItY6lnath/1B8vw4Ob1T0vY0FLCR7UcpQxPok6VRWZhEqo8wW+ucK3dVt ZiDdNLjQxAhSZYOAVKpMWIlMaFh2V0SHfvEFr5XMRVfXBHg/EY+Ujewcgs3vfCcHOKaT vGRLKqV/mqfN3+4qmEOpcx57Tc4h4rHUbzm7zMESk8VTjT3pNJB5wi5RW9AF83anSHqX orzA== X-Gm-Message-State: AOAM532Crn5AB4KG22Mv4u6SogDajSDw0rRW/EzO7sRsuW/QOZZXTsW6 GEGwhiymTf2gg6yxYu+bL36wo36ObIuuJqdf X-Google-Smtp-Source: ABdhPJz5kEYvrH0IyW2BEgOwhf1W7ehe5KwvGaoplM2GFEDpylyB+pSL6zv7EhCtKHSEp2UtuVxfew== X-Received: by 2002:a1c:f316:: with SMTP id q22mr22106612wmq.55.1635022322468; Sat, 23 Oct 2021 13:52:02 -0700 (PDT) Received: from localhost.localdomain ([149.86.74.50]) by smtp.gmail.com with ESMTPSA id u16sm13555398wmc.21.2021.10.23.13.52.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Oct 2021 13:52:02 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet Subject: [PATCH bpf-next v2 2/5] bpftool: Do not expose and init hash maps for pinned path in main.c Date: Sat, 23 Oct 2021 21:51:51 +0100 Message-Id: <20211023205154.6710-3-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211023205154.6710-1-quentin@isovalent.com> References: <20211023205154.6710-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net BPF programs, maps, and links, can all be listed with their pinned paths by bpftool, when the "-f" option is provided. To do so, bpftool builds hash maps containing all pinned paths for each kind of objects. These three hash maps are always initialised in main.c, and exposed through main.h. There appear to be no particular reason to do so: we can just as well make them static to the files that need them (prog.c, map.c, and link.c respectively), and initialise them only when we want to show objects and the "-f" switch is provided. This may prevent unnecessary memory allocations if the implementation of the hash maps was to change in the future. Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/link.c | 9 ++++++++- tools/bpf/bpftool/main.c | 12 ------------ tools/bpf/bpftool/main.h | 3 --- tools/bpf/bpftool/map.c | 9 ++++++++- tools/bpf/bpftool/prog.c | 9 ++++++++- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 8cc3e36f8cc6..a5effb1816b7 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -20,6 +20,8 @@ static const char * const link_type_name[] = { [BPF_LINK_TYPE_NETNS] = "netns", }; +static struct pinned_obj_table link_table; + static int link_parse_fd(int *argc, char ***argv) { int fd; @@ -302,8 +304,10 @@ static int do_show(int argc, char **argv) __u32 id = 0; int err, fd; - if (show_pinned) + if (show_pinned) { + hash_init(link_table.table); build_pinned_obj_table(&link_table, BPF_OBJ_LINK); + } build_obj_refs_table(&refs_table, BPF_OBJ_LINK); if (argc == 2) { @@ -346,6 +350,9 @@ static int do_show(int argc, char **argv) delete_obj_refs_table(&refs_table); + if (show_pinned) + delete_pinned_obj_table(&link_table); + return errno == ENOENT ? 0 : -1; } diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c index 02eaaf065f65..7a33f0e6da28 100644 --- a/tools/bpf/bpftool/main.c +++ b/tools/bpf/bpftool/main.c @@ -31,9 +31,6 @@ bool verifier_logs; bool relaxed_maps; bool use_loader; struct btf *base_btf; -struct pinned_obj_table prog_table; -struct pinned_obj_table map_table; -struct pinned_obj_table link_table; struct obj_refs_table refs_table; static void __noreturn clean_and_exit(int i) @@ -409,10 +406,6 @@ int main(int argc, char **argv) block_mount = false; bin_name = argv[0]; - hash_init(prog_table.table); - hash_init(map_table.table); - hash_init(link_table.table); - opterr = 0; while ((opt = getopt_long(argc, argv, "VhpjfLmndB:", options, NULL)) >= 0) { @@ -479,11 +472,6 @@ int main(int argc, char **argv) if (json_output) jsonw_destroy(&json_wtr); - if (show_pinned) { - delete_pinned_obj_table(&prog_table); - delete_pinned_obj_table(&map_table); - delete_pinned_obj_table(&link_table); - } btf__free(base_btf); return ret; diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index 90caa42aac4c..baf607cd5924 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -91,9 +91,6 @@ extern bool verifier_logs; extern bool relaxed_maps; extern bool use_loader; extern struct btf *base_btf; -extern struct pinned_obj_table prog_table; -extern struct pinned_obj_table map_table; -extern struct pinned_obj_table link_table; extern struct obj_refs_table refs_table; void __printf(1, 2) p_err(const char *fmt, ...); diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 407071d54ab1..0085039d9610 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -56,6 +56,8 @@ const char * const map_type_name[] = { const size_t map_type_name_size = ARRAY_SIZE(map_type_name); +static struct pinned_obj_table map_table; + static bool map_is_per_cpu(__u32 type) { return type == BPF_MAP_TYPE_PERCPU_HASH || @@ -694,8 +696,10 @@ static int do_show(int argc, char **argv) int err; int fd; - if (show_pinned) + if (show_pinned) { + hash_init(map_table.table); build_pinned_obj_table(&map_table, BPF_OBJ_MAP); + } build_obj_refs_table(&refs_table, BPF_OBJ_MAP); if (argc == 2) @@ -742,6 +746,9 @@ static int do_show(int argc, char **argv) delete_obj_refs_table(&refs_table); + if (show_pinned) + delete_pinned_obj_table(&map_table); + return errno == ENOENT ? 0 : -1; } diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index f633299b1261..48c2fa4d068e 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -84,6 +84,8 @@ static const char * const attach_type_strings[] = { [__MAX_BPF_ATTACH_TYPE] = NULL, }; +static struct pinned_obj_table prog_table; + static enum bpf_attach_type parse_attach_type(const char *str) { enum bpf_attach_type type; @@ -567,8 +569,10 @@ static int do_show(int argc, char **argv) int err; int fd; - if (show_pinned) + if (show_pinned) { + hash_init(prog_table.table); build_pinned_obj_table(&prog_table, BPF_OBJ_PROG); + } build_obj_refs_table(&refs_table, BPF_OBJ_PROG); if (argc == 2) @@ -613,6 +617,9 @@ static int do_show(int argc, char **argv) delete_obj_refs_table(&refs_table); + if (show_pinned) + delete_pinned_obj_table(&prog_table); + return err; } From patchwork Sat Oct 23 20:51:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12579835 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DE2A7C4321E for ; Sat, 23 Oct 2021 20:52:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C578160E75 for ; Sat, 23 Oct 2021 20:52:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231376AbhJWUy3 (ORCPT ); Sat, 23 Oct 2021 16:54:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231290AbhJWUy1 (ORCPT ); Sat, 23 Oct 2021 16:54:27 -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 E9D55C061224 for ; Sat, 23 Oct 2021 13:52:04 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id v17so3357285wrv.9 for ; Sat, 23 Oct 2021 13:52:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bnsaoE1e74DLOSAtR3sU8+tx3L1vl2315dJJbbFqhwA=; b=xk1sp6FCgFAnCZkSENGff/VbflX4iTBSz2KUmXUhJCUDyUVRfwgIrMPLBv7EmXZPjO chbHeTGOpixKlIRopb7I35yB7OgEDfJGu42cRdHblcbM8E7FChRFyhNhKVr/bMLI0PFR +lQem7iLZ+l9taKeuA4DgHACbG6bbhRs8tL0v+nX7wI4fvwIr2Nr2vMkTw5xG9RkmtCZ zw8qV4tZNs3/1ORhfNQ4f4hmRhv3ip9k4fHghGz3gkpZzXEebv/mQzHDl+vBws5mbNiJ zcNQ1QcLAN1Fupoc7y+skKe16Bx4+I+/KyX7WOpwXOBa70JfTIIsVGLREYmG1SQ2niAJ ciKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bnsaoE1e74DLOSAtR3sU8+tx3L1vl2315dJJbbFqhwA=; b=iqSYt+NWGPdiuq8nsP1S/7jZwNeZT+ORCffyeMxK5au8Y2YbqCmbuwW9viVuwNRsme 5vsAnZM0GRc0NKvYzV4dLgCEZtO9h05ct+8rhowehxKSXlcoA3AX8MFjGU+nTz2YhBwZ bWmWuUNzxrpzB/iPU1afSdxoY3jUzh1cCy2XGYKSD3iyys5Ycf7dWg+I3tMmmHIt+Lw3 E7cc9K8AcW+tsuAeP4LflLyuYCd4kDGQm635Pd0XNt0/gbr3NnG042AL6h4ur8DVeitD Pb4fwUSzz59W1kfrw52yko9TL60kjjaL3TRWKqEs7k7HzTZVJOkMB/DZHKRHl6qzdgM9 +BEQ== X-Gm-Message-State: AOAM5309QZB1D3oZem3KrHDlObpUVEYMqxLNGOw2x5G2AOPYO+XPQaoA p7RU9KU+OGBSAmuwGeYzZ5tdQg== X-Google-Smtp-Source: ABdhPJy8zfqQ9Kcn1jlxAOcN8Jp5M1mwAfrL6QI3aDVgXlEDmmdWlPojEETFIIV82GeNBlEy7nBoPg== X-Received: by 2002:a05:6000:2a1:: with SMTP id l1mr10366862wry.87.1635022323449; Sat, 23 Oct 2021 13:52:03 -0700 (PDT) Received: from localhost.localdomain ([149.86.74.50]) by smtp.gmail.com with ESMTPSA id u16sm13555398wmc.21.2021.10.23.13.52.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Oct 2021 13:52:03 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet Subject: [PATCH bpf-next v2 3/5] bpftool: Switch to libbpf's hashmap for pinned paths of BPF objects Date: Sat, 23 Oct 2021 21:51:52 +0100 Message-Id: <20211023205154.6710-4-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211023205154.6710-1-quentin@isovalent.com> References: <20211023205154.6710-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net In order to show pinned paths for BPF programs, maps, or links when listing them with the "-f" option, bpftool creates hash maps to store all relevant paths under the bpffs. So far, it would rely on the kernel implementation (from tools/include/linux/hashtable.h). We can make bpftool rely on libbpf's implementation instead. The motivation is to make bpftool less dependent of kernel headers, to ease the path to a potential out-of-tree mirror, like libbpf has. This commit is the first step of the conversion: the hash maps for pinned paths for programs, maps, and links are converted to libbpf's hashmap.{c,h}. Other hash maps used for the PIDs of process holding references to BPF objects are left unchanged for now. On the build side, this requires adding a dependency to a second header internal to libbpf, and making it a dependency for the bootstrap bpftool version as well. The rest of the changes are a rather straightforward conversion. Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/Makefile | 8 +++--- tools/bpf/bpftool/common.c | 50 +++++++++++++++++++++++--------------- tools/bpf/bpftool/link.c | 36 +++++++++++++++------------ tools/bpf/bpftool/main.h | 29 +++++++++++++--------- tools/bpf/bpftool/map.c | 36 +++++++++++++++------------ tools/bpf/bpftool/prog.c | 36 +++++++++++++++------------ 6 files changed, 111 insertions(+), 84 deletions(-) diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 939b0fca5fb9..c0c30e56988f 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile @@ -31,9 +31,9 @@ LIBBPF = $(LIBBPF_OUTPUT)libbpf.a LIBBPF_BOOTSTRAP_OUTPUT = $(BOOTSTRAP_OUTPUT)libbpf/ LIBBPF_BOOTSTRAP = $(LIBBPF_BOOTSTRAP_OUTPUT)libbpf.a -# We need to copy nlattr.h which is not otherwise exported by libbpf, but still -# required by bpftool. -LIBBPF_INTERNAL_HDRS := $(addprefix $(LIBBPF_HDRS_DIR)/,nlattr.h) +# We need to copy hashmap.h and nlattr.h which is not otherwise exported by +# libbpf, but still required by bpftool. +LIBBPF_INTERNAL_HDRS := $(addprefix $(LIBBPF_HDRS_DIR)/,hashmap.h nlattr.h) ifeq ($(BPFTOOL_VERSION),) BPFTOOL_VERSION := $(shell make -rR --no-print-directory -sC ../../.. kernelversion) @@ -209,7 +209,7 @@ $(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP) $(OUTPUT)bpftool: $(OBJS) $(LIBBPF) $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) -$(BOOTSTRAP_OUTPUT)%.o: %.c | $(BOOTSTRAP_OUTPUT) +$(BOOTSTRAP_OUTPUT)%.o: %.c $(LIBBPF_INTERNAL_HDRS) | $(BOOTSTRAP_OUTPUT) $(QUIET_CC)$(HOSTCC) $(CFLAGS) -c -MMD -o $@ $< $(OUTPUT)%.o: %.c diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index d42d930a3ec4..511eccdbdfe6 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -22,6 +22,7 @@ #include #include +#include #include /* libbpf_num_possible_cpus */ #include "main.h" @@ -393,7 +394,7 @@ void print_hex_data_json(uint8_t *data, size_t len) } /* extra params for nftw cb */ -static struct pinned_obj_table *build_fn_table; +static struct hashmap *build_fn_table; static enum bpf_obj_type build_fn_type; static int do_build_table_cb(const char *fpath, const struct stat *sb, @@ -401,9 +402,9 @@ static int do_build_table_cb(const char *fpath, const struct stat *sb, { struct bpf_prog_info pinned_info; __u32 len = sizeof(pinned_info); - struct pinned_obj *obj_node; enum bpf_obj_type objtype; int fd, err = 0; + char *path; if (typeflag != FTW_F) goto out_ret; @@ -420,28 +421,26 @@ static int do_build_table_cb(const char *fpath, const struct stat *sb, if (bpf_obj_get_info_by_fd(fd, &pinned_info, &len)) goto out_close; - obj_node = calloc(1, sizeof(*obj_node)); - if (!obj_node) { + path = strdup(fpath); + if (!path) { err = -1; goto out_close; } - obj_node->id = pinned_info.id; - obj_node->path = strdup(fpath); - if (!obj_node->path) { - err = -1; - free(obj_node); + err = hashmap__append(build_fn_table, u32_as_hash_field(pinned_info.id), path); + if (err) { + p_err("failed to append entry to hashmap for ID %u, path '%s': %s", + pinned_info.id, path, strerror(errno)); goto out_close; } - hash_add(build_fn_table->table, &obj_node->hash, obj_node->id); out_close: close(fd); out_ret: return err; } -int build_pinned_obj_table(struct pinned_obj_table *tab, +int build_pinned_obj_table(struct hashmap *tab, enum bpf_obj_type type) { struct mntent *mntent = NULL; @@ -470,17 +469,18 @@ int build_pinned_obj_table(struct pinned_obj_table *tab, return err; } -void delete_pinned_obj_table(struct pinned_obj_table *tab) +void delete_pinned_obj_table(struct hashmap *map) { - struct pinned_obj *obj; - struct hlist_node *tmp; - unsigned int bkt; + struct hashmap_entry *entry; + size_t bkt; - hash_for_each_safe(tab->table, bkt, tmp, obj, hash) { - hash_del(&obj->hash); - free(obj->path); - free(obj); - } + if (!map) + return; + + hashmap__for_each_entry(map, entry, bkt) + free(entry->value); + + hashmap__free(map); } unsigned int get_page_size(void) @@ -962,3 +962,13 @@ int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len) return fd; } + +size_t hash_fn_for_key_as_id(const void *key, void *ctx) +{ + return (size_t)key; +} + +bool equal_fn_for_key_as_id(const void *k1, const void *k2, void *ctx) +{ + return k1 == k2; +} diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index a5effb1816b7..404cc5459c6b 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -7,6 +7,7 @@ #include #include +#include #include "json_writer.h" #include "main.h" @@ -20,7 +21,7 @@ static const char * const link_type_name[] = { [BPF_LINK_TYPE_NETNS] = "netns", }; -static struct pinned_obj_table link_table; +static struct hashmap *link_table; static int link_parse_fd(int *argc, char ***argv) { @@ -158,15 +159,14 @@ static int show_link_close_json(int fd, struct bpf_link_info *info) break; } - if (!hash_empty(link_table.table)) { - struct pinned_obj *obj; + if (!hashmap__empty(link_table)) { + struct hashmap_entry *entry; jsonw_name(json_wtr, "pinned"); jsonw_start_array(json_wtr); - hash_for_each_possible(link_table.table, obj, hash, info->id) { - if (obj->id == info->id) - jsonw_string(json_wtr, obj->path); - } + hashmap__for_each_key_entry(link_table, entry, + u32_as_hash_field(info->id)) + jsonw_string(json_wtr, entry->value); jsonw_end_array(json_wtr); } @@ -246,13 +246,12 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info) break; } - if (!hash_empty(link_table.table)) { - struct pinned_obj *obj; + if (!hashmap__empty(link_table)) { + struct hashmap_entry *entry; - hash_for_each_possible(link_table.table, obj, hash, info->id) { - if (obj->id == info->id) - printf("\n\tpinned %s", obj->path); - } + hashmap__for_each_key_entry(link_table, entry, + u32_as_hash_field(info->id)) + printf("\n\tpinned %s", (char *)entry->value); } emit_obj_refs_plain(&refs_table, info->id, "\n\tpids "); @@ -305,8 +304,13 @@ static int do_show(int argc, char **argv) int err, fd; if (show_pinned) { - hash_init(link_table.table); - build_pinned_obj_table(&link_table, BPF_OBJ_LINK); + link_table = hashmap__new(hash_fn_for_key_as_id, + equal_fn_for_key_as_id, NULL); + if (!link_table) { + p_err("failed to create hashmap for pinned paths"); + return -1; + } + build_pinned_obj_table(link_table, BPF_OBJ_LINK); } build_obj_refs_table(&refs_table, BPF_OBJ_LINK); @@ -351,7 +355,7 @@ static int do_show(int argc, char **argv) delete_obj_refs_table(&refs_table); if (show_pinned) - delete_pinned_obj_table(&link_table); + delete_pinned_obj_table(link_table); return errno == ENOENT ? 0 : -1; } diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index baf607cd5924..0d64e39573f2 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -14,6 +14,7 @@ #include #include +#include #include #include "json_writer.h" @@ -105,16 +106,6 @@ void set_max_rlimit(void); int mount_tracefs(const char *target); -struct pinned_obj_table { - DECLARE_HASHTABLE(table, 16); -}; - -struct pinned_obj { - __u32 id; - char *path; - struct hlist_node hash; -}; - struct obj_refs_table { DECLARE_HASHTABLE(table, 16); }; @@ -134,9 +125,9 @@ struct obj_refs { struct btf; struct bpf_line_info; -int build_pinned_obj_table(struct pinned_obj_table *table, +int build_pinned_obj_table(struct hashmap *table, enum bpf_obj_type type); -void delete_pinned_obj_table(struct pinned_obj_table *tab); +void delete_pinned_obj_table(struct hashmap *table); __weak int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type); __weak void delete_obj_refs_table(struct obj_refs_table *table); @@ -256,4 +247,18 @@ int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind, int print_all_levels(__maybe_unused enum libbpf_print_level level, const char *format, va_list args); + +size_t hash_fn_for_key_as_id(const void *key, void *ctx); +bool equal_fn_for_key_as_id(const void *k1, const void *k2, void *ctx); + +static inline void *u32_as_hash_field(__u32 x) +{ + return (void *)(uintptr_t)x; +} + +static inline bool hashmap__empty(struct hashmap *map) +{ + return map ? hashmap__size(map) == 0 : true; +} + #endif diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 0085039d9610..2647603c5e5d 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -17,6 +17,7 @@ #include #include +#include #include "json_writer.h" #include "main.h" @@ -56,7 +57,7 @@ const char * const map_type_name[] = { const size_t map_type_name_size = ARRAY_SIZE(map_type_name); -static struct pinned_obj_table map_table; +static struct hashmap *map_table; static bool map_is_per_cpu(__u32 type) { @@ -537,15 +538,14 @@ static int show_map_close_json(int fd, struct bpf_map_info *info) if (info->btf_id) jsonw_int_field(json_wtr, "btf_id", info->btf_id); - if (!hash_empty(map_table.table)) { - struct pinned_obj *obj; + if (!hashmap__empty(map_table)) { + struct hashmap_entry *entry; jsonw_name(json_wtr, "pinned"); jsonw_start_array(json_wtr); - hash_for_each_possible(map_table.table, obj, hash, info->id) { - if (obj->id == info->id) - jsonw_string(json_wtr, obj->path); - } + hashmap__for_each_key_entry(map_table, entry, + u32_as_hash_field(info->id)) + jsonw_string(json_wtr, entry->value); jsonw_end_array(json_wtr); } @@ -612,13 +612,12 @@ static int show_map_close_plain(int fd, struct bpf_map_info *info) } close(fd); - if (!hash_empty(map_table.table)) { - struct pinned_obj *obj; + if (!hashmap__empty(map_table)) { + struct hashmap_entry *entry; - hash_for_each_possible(map_table.table, obj, hash, info->id) { - if (obj->id == info->id) - printf("\n\tpinned %s", obj->path); - } + hashmap__for_each_key_entry(map_table, entry, + u32_as_hash_field(info->id)) + printf("\n\tpinned %s", (char *)entry->value); } printf("\n"); @@ -697,8 +696,13 @@ static int do_show(int argc, char **argv) int fd; if (show_pinned) { - hash_init(map_table.table); - build_pinned_obj_table(&map_table, BPF_OBJ_MAP); + map_table = hashmap__new(hash_fn_for_key_as_id, + equal_fn_for_key_as_id, NULL); + if (!map_table) { + p_err("failed to create hashmap for pinned paths"); + return -1; + } + build_pinned_obj_table(map_table, BPF_OBJ_MAP); } build_obj_refs_table(&refs_table, BPF_OBJ_MAP); @@ -747,7 +751,7 @@ static int do_show(int argc, char **argv) delete_obj_refs_table(&refs_table); if (show_pinned) - delete_pinned_obj_table(&map_table); + delete_pinned_obj_table(map_table); return errno == ENOENT ? 0 : -1; } diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index 48c2fa4d068e..8fce78ce6f8b 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -84,7 +85,7 @@ static const char * const attach_type_strings[] = { [__MAX_BPF_ATTACH_TYPE] = NULL, }; -static struct pinned_obj_table prog_table; +static struct hashmap *prog_table; static enum bpf_attach_type parse_attach_type(const char *str) { @@ -418,15 +419,14 @@ static void print_prog_json(struct bpf_prog_info *info, int fd) if (info->btf_id) jsonw_int_field(json_wtr, "btf_id", info->btf_id); - if (!hash_empty(prog_table.table)) { - struct pinned_obj *obj; + if (!hashmap__empty(prog_table)) { + struct hashmap_entry *entry; jsonw_name(json_wtr, "pinned"); jsonw_start_array(json_wtr); - hash_for_each_possible(prog_table.table, obj, hash, info->id) { - if (obj->id == info->id) - jsonw_string(json_wtr, obj->path); - } + hashmap__for_each_key_entry(prog_table, entry, + u32_as_hash_field(info->id)) + jsonw_string(json_wtr, entry->value); jsonw_end_array(json_wtr); } @@ -490,13 +490,12 @@ static void print_prog_plain(struct bpf_prog_info *info, int fd) if (info->nr_map_ids) show_prog_maps(fd, info->nr_map_ids); - if (!hash_empty(prog_table.table)) { - struct pinned_obj *obj; + if (!hashmap__empty(prog_table)) { + struct hashmap_entry *entry; - hash_for_each_possible(prog_table.table, obj, hash, info->id) { - if (obj->id == info->id) - printf("\n\tpinned %s", obj->path); - } + hashmap__for_each_key_entry(prog_table, entry, + u32_as_hash_field(info->id)) + printf("\n\tpinned %s", (char *)entry->value); } if (info->btf_id) @@ -570,8 +569,13 @@ static int do_show(int argc, char **argv) int fd; if (show_pinned) { - hash_init(prog_table.table); - build_pinned_obj_table(&prog_table, BPF_OBJ_PROG); + prog_table = hashmap__new(hash_fn_for_key_as_id, + equal_fn_for_key_as_id, NULL); + if (!prog_table) { + p_err("failed to create hashmap for pinned paths"); + return -1; + } + build_pinned_obj_table(prog_table, BPF_OBJ_PROG); } build_obj_refs_table(&refs_table, BPF_OBJ_PROG); @@ -618,7 +622,7 @@ static int do_show(int argc, char **argv) delete_obj_refs_table(&refs_table); if (show_pinned) - delete_pinned_obj_table(&prog_table); + delete_pinned_obj_table(prog_table); return err; } From patchwork Sat Oct 23 20:51:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12579837 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F874C433FE for ; Sat, 23 Oct 2021 20:52:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5D1C860FBF for ; Sat, 23 Oct 2021 20:52:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231391AbhJWUya (ORCPT ); Sat, 23 Oct 2021 16:54:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231304AbhJWUy1 (ORCPT ); Sat, 23 Oct 2021 16:54:27 -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 C92FCC061226 for ; Sat, 23 Oct 2021 13:52:05 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id z11-20020a1c7e0b000000b0030db7b70b6bso8165814wmc.1 for ; Sat, 23 Oct 2021 13:52:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Gh1KmHecNhkMjMGwCdkMHK2Zl6nIhGiYjkV1WWdBFHg=; b=F7WrId1oogJWRwY/u07zvKSmmdXj9qjFc8O0kpijptrkqQN0XpXtOqNruF08fN7Hd9 LQ1sIA6ygw0fEiq6MDEU/iXTqugLFMAFwTE7W7RCZ3R+HPIKR+y11FsDsp1uQXRuLQ4I lDYs+RsD2SVyNNXUnJdOVHSTre5BxkB5/t823jMJ91fmu2pooUOsvESJ/pJ7Okk/dI5Y +Zjo99c33nrTFrbIiG/fDz1swULsYBZTE8sZeZxelp4IwFejB9WtaFFQLbi1yQaEo+Tk JU9gHOmm6oVUWJQy9yt54GzzqmfQCyqOLIzfe+iisH3mkO2duGDlIn3gg4jtVeRmRwpr naeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Gh1KmHecNhkMjMGwCdkMHK2Zl6nIhGiYjkV1WWdBFHg=; b=ZDIjJlxyuUYZCT8syALe+J+kinPEuxiAAXAux8DBl7OkrAMXEMG4u+iVX7JlsMQ7lI 8UaxfFjs3ObB56rApKhcMOw3Qt+FDHzfuBcgbrHIcN24fEOU8k6HdqVwf36ytDb0ZiTz EOV1sO+bhbrnVjb/qKr0fnZBBXzN+bvuv/bO183mX71MJOurZbawaILkhse9/sid/X7M q68jeCBz+sWxwRY1Mo7Gkk8j3yAah+fV8sM/+AadlXrnzuQrZwxKwkZZaz2bx4Tdlcat OflnX2Cpq+TW5v5VTR+8w3nlR0cBqAJSD5rH5vU6fj8+u1MBowz43pFoQHQqNPZo2iQP 1gBA== X-Gm-Message-State: AOAM5313xWaXeeLXlTDcaHG/T3CTVTDn2sZnERKofYka3usKkjlxv93K HOnVYwBSB/NULK3oKGC4XZ2Jqw== X-Google-Smtp-Source: ABdhPJwoxmAz7SO8PouYUh2EChJNsZbcDb+Nv5fyEBNC/jlmVNcxKEnHsK+044ces20C9QghVhVM6A== X-Received: by 2002:a1c:7e87:: with SMTP id z129mr8967749wmc.75.1635022324447; Sat, 23 Oct 2021 13:52:04 -0700 (PDT) Received: from localhost.localdomain ([149.86.74.50]) by smtp.gmail.com with ESMTPSA id u16sm13555398wmc.21.2021.10.23.13.52.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Oct 2021 13:52:03 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet Subject: [PATCH bpf-next v2 4/5] bpftool: Switch to libbpf's hashmap for programs/maps in BTF listing Date: Sat, 23 Oct 2021 21:51:53 +0100 Message-Id: <20211023205154.6710-5-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211023205154.6710-1-quentin@isovalent.com> References: <20211023205154.6710-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net In order to show BPF programs and maps using BTF objects when the latter are being listed, bpftool creates hash maps to store all relevant items. This commit is part of a set that transitions from the kernel's hash map implementation to the one coming with libbpf. The motivation is to make bpftool less dependent of kernel headers, to ease the path to a potential out-of-tree mirror, like libbpf has. This commit focuses on the two hash maps used by bpftool when listing BTF objects to store references to programs and maps, and convert them to the libbpf's implementation. Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/btf.c | 125 ++++++++++++++++++--------------------- tools/bpf/bpftool/main.h | 5 ++ 2 files changed, 62 insertions(+), 68 deletions(-) diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c index 0cd769adac66..11dd31b6e730 100644 --- a/tools/bpf/bpftool/btf.c +++ b/tools/bpf/bpftool/btf.c @@ -8,14 +8,16 @@ #include #include #include -#include -#include -#include #include #include #include #include +#include +#include +#include +#include + #include "json_writer.h" #include "main.h" @@ -40,14 +42,9 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = { [BTF_KIND_DECL_TAG] = "DECL_TAG", }; -struct btf_attach_table { - DECLARE_HASHTABLE(table, 16); -}; - struct btf_attach_point { __u32 obj_id; __u32 btf_id; - struct hlist_node hash; }; static const char *btf_int_enc_str(__u8 encoding) @@ -645,21 +642,8 @@ static int btf_parse_fd(int *argc, char ***argv) return fd; } -static void delete_btf_table(struct btf_attach_table *tab) -{ - struct btf_attach_point *obj; - struct hlist_node *tmp; - - unsigned int bkt; - - hash_for_each_safe(tab->table, bkt, tmp, obj, hash) { - hash_del(&obj->hash); - free(obj); - } -} - static int -build_btf_type_table(struct btf_attach_table *tab, enum bpf_obj_type type, +build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type, void *info, __u32 *len) { static const char * const names[] = { @@ -667,7 +651,6 @@ build_btf_type_table(struct btf_attach_table *tab, enum bpf_obj_type type, [BPF_OBJ_PROG] = "prog", [BPF_OBJ_MAP] = "map", }; - struct btf_attach_point *obj_node; __u32 btf_id, id = 0; int err; int fd; @@ -741,28 +724,25 @@ build_btf_type_table(struct btf_attach_table *tab, enum bpf_obj_type type, if (!btf_id) continue; - obj_node = calloc(1, sizeof(*obj_node)); - if (!obj_node) { - p_err("failed to allocate memory: %s", strerror(errno)); - err = -ENOMEM; + err = hashmap__append(tab, u32_as_hash_field(btf_id), + u32_as_hash_field(id)); + if (err) { + p_err("failed to append entry to hashmap for BTF ID %u, object ID %u: %s", + btf_id, id, strerror(errno)); goto err_free; } - - obj_node->obj_id = id; - obj_node->btf_id = btf_id; - hash_add(tab->table, &obj_node->hash, obj_node->btf_id); } return 0; err_free: - delete_btf_table(tab); + hashmap__free(tab); return err; } static int -build_btf_tables(struct btf_attach_table *btf_prog_table, - struct btf_attach_table *btf_map_table) +build_btf_tables(struct hashmap *btf_prog_table, + struct hashmap *btf_map_table) { struct bpf_prog_info prog_info; __u32 prog_len = sizeof(prog_info); @@ -778,7 +758,7 @@ build_btf_tables(struct btf_attach_table *btf_prog_table, err = build_btf_type_table(btf_map_table, BPF_OBJ_MAP, &map_info, &map_len); if (err) { - delete_btf_table(btf_prog_table); + hashmap__free(btf_prog_table); return err; } @@ -787,10 +767,10 @@ build_btf_tables(struct btf_attach_table *btf_prog_table, static void show_btf_plain(struct bpf_btf_info *info, int fd, - struct btf_attach_table *btf_prog_table, - struct btf_attach_table *btf_map_table) + struct hashmap *btf_prog_table, + struct hashmap *btf_map_table) { - struct btf_attach_point *obj; + struct hashmap_entry *entry; const char *name = u64_to_ptr(info->name); int n; @@ -804,18 +784,19 @@ show_btf_plain(struct bpf_btf_info *info, int fd, printf("size %uB", info->btf_size); n = 0; - hash_for_each_possible(btf_prog_table->table, obj, hash, info->id) { - if (obj->btf_id == info->id) - printf("%s%u", n++ == 0 ? " prog_ids " : ",", - obj->obj_id); + hashmap__for_each_key_entry(btf_prog_table, entry, + u32_as_hash_field(info->id)) { + printf("%s%u", n++ == 0 ? " prog_ids " : ",", + hash_field_as_u32(entry->value)); } n = 0; - hash_for_each_possible(btf_map_table->table, obj, hash, info->id) { - if (obj->btf_id == info->id) - printf("%s%u", n++ == 0 ? " map_ids " : ",", - obj->obj_id); + hashmap__for_each_key_entry(btf_map_table, entry, + u32_as_hash_field(info->id)) { + printf("%s%u", n++ == 0 ? " map_ids " : ",", + hash_field_as_u32(entry->value)); } + emit_obj_refs_plain(&refs_table, info->id, "\n\tpids "); printf("\n"); @@ -823,10 +804,10 @@ show_btf_plain(struct bpf_btf_info *info, int fd, static void show_btf_json(struct bpf_btf_info *info, int fd, - struct btf_attach_table *btf_prog_table, - struct btf_attach_table *btf_map_table) + struct hashmap *btf_prog_table, + struct hashmap *btf_map_table) { - struct btf_attach_point *obj; + struct hashmap_entry *entry; const char *name = u64_to_ptr(info->name); jsonw_start_object(json_wtr); /* btf object */ @@ -835,19 +816,17 @@ show_btf_json(struct bpf_btf_info *info, int fd, jsonw_name(json_wtr, "prog_ids"); jsonw_start_array(json_wtr); /* prog_ids */ - hash_for_each_possible(btf_prog_table->table, obj, hash, - info->id) { - if (obj->btf_id == info->id) - jsonw_uint(json_wtr, obj->obj_id); + hashmap__for_each_key_entry(btf_prog_table, entry, + u32_as_hash_field(info->id)) { + jsonw_uint(json_wtr, hash_field_as_u32(entry->value)); } jsonw_end_array(json_wtr); /* prog_ids */ jsonw_name(json_wtr, "map_ids"); jsonw_start_array(json_wtr); /* map_ids */ - hash_for_each_possible(btf_map_table->table, obj, hash, - info->id) { - if (obj->btf_id == info->id) - jsonw_uint(json_wtr, obj->obj_id); + hashmap__for_each_key_entry(btf_map_table, entry, + u32_as_hash_field(info->id)) { + jsonw_uint(json_wtr, hash_field_as_u32(entry->value)); } jsonw_end_array(json_wtr); /* map_ids */ @@ -862,8 +841,8 @@ show_btf_json(struct bpf_btf_info *info, int fd, } static int -show_btf(int fd, struct btf_attach_table *btf_prog_table, - struct btf_attach_table *btf_map_table) +show_btf(int fd, struct hashmap *btf_prog_table, + struct hashmap *btf_map_table) { struct bpf_btf_info info; __u32 len = sizeof(info); @@ -900,8 +879,8 @@ show_btf(int fd, struct btf_attach_table *btf_prog_table, static int do_show(int argc, char **argv) { - struct btf_attach_table btf_prog_table; - struct btf_attach_table btf_map_table; + struct hashmap *btf_prog_table; + struct hashmap *btf_map_table; int err, fd = -1; __u32 id = 0; @@ -917,9 +896,19 @@ static int do_show(int argc, char **argv) return BAD_ARG(); } - hash_init(btf_prog_table.table); - hash_init(btf_map_table.table); - err = build_btf_tables(&btf_prog_table, &btf_map_table); + btf_prog_table = hashmap__new(hash_fn_for_key_as_id, + equal_fn_for_key_as_id, NULL); + btf_map_table = hashmap__new(hash_fn_for_key_as_id, + equal_fn_for_key_as_id, NULL); + if (!btf_prog_table || !btf_map_table) { + hashmap__free(btf_prog_table); + hashmap__free(btf_map_table); + if (fd >= 0) + close(fd); + p_err("failed to create hashmap for object references"); + return -1; + } + err = build_btf_tables(btf_prog_table, btf_map_table); if (err) { if (fd >= 0) close(fd); @@ -928,7 +917,7 @@ static int do_show(int argc, char **argv) build_obj_refs_table(&refs_table, BPF_OBJ_BTF); if (fd >= 0) { - err = show_btf(fd, &btf_prog_table, &btf_map_table); + err = show_btf(fd, btf_prog_table, btf_map_table); close(fd); goto exit_free; } @@ -960,7 +949,7 @@ static int do_show(int argc, char **argv) break; } - err = show_btf(fd, &btf_prog_table, &btf_map_table); + err = show_btf(fd, btf_prog_table, btf_map_table); close(fd); if (err) break; @@ -970,8 +959,8 @@ static int do_show(int argc, char **argv) jsonw_end_array(json_wtr); /* root array */ exit_free: - delete_btf_table(&btf_prog_table); - delete_btf_table(&btf_map_table); + hashmap__free(btf_prog_table); + hashmap__free(btf_map_table); delete_obj_refs_table(&refs_table); return err; diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index 0d64e39573f2..f6f2f951b193 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -256,6 +256,11 @@ static inline void *u32_as_hash_field(__u32 x) return (void *)(uintptr_t)x; } +static inline __u32 hash_field_as_u32(const void *x) +{ + return (__u32)(uintptr_t)x; +} + static inline bool hashmap__empty(struct hashmap *map) { return map ? hashmap__size(map) == 0 : true; From patchwork Sat Oct 23 20:51:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12579839 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90481C433F5 for ; Sat, 23 Oct 2021 20:52:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 749C660E75 for ; Sat, 23 Oct 2021 20:52:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231271AbhJWUyd (ORCPT ); Sat, 23 Oct 2021 16:54:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231319AbhJWUy1 (ORCPT ); Sat, 23 Oct 2021 16:54:27 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDA98C061228 for ; Sat, 23 Oct 2021 13:52:06 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id b133-20020a1c808b000000b0032ca4d18aebso4377203wmd.2 for ; Sat, 23 Oct 2021 13:52:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=V75jyjYdnsS07mbviL23NDsfFATkmATGP+SmihVgCnc=; b=DJom9nNiqcagruhi2MeS37eTM40tr0+0XVvXdeibkuOp5ukBiUc2tM18tg95jGr5kq 0wV9O771pci7gQTVRJAVLuIojm8myBNNdJAtwAh5TpT/yj6HfUi/fqdnKEqxIIVcTq8D VKg/nDniritOPObV1x5Cy6ulVwXyhWigJPXs0LhgBZI64Z3HR13CAoIroXD//pIYouG1 NeyxqT8XaRDXKreDUj5ub7TbXxK978AzKipSGaymz5JPI9qOAo8p/m/bm9lWVObBf70q uoRcs/KuqvIE5WC89JhD05y6KG1er6df12fyUR2NJ7V2p9NVR7aXywWEbIo9bf+yDVWK 7oFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=V75jyjYdnsS07mbviL23NDsfFATkmATGP+SmihVgCnc=; b=B6LPSSibIfot0Uya9rQCNq4K+KURHRfyXBJmXpfakHINSXQ7Qf/Oux3c3ZVzpTdnmY fakEsKFgGXniVLGd8QkWFqflo5UJ0lG61/BQ7yqEz9jA5cdopsudCve/mVSQcRBaf1qv RYCeKPQHXRyZAbhf7EvsUHDqRnPCN7TF9r9B62dDuoJj/IssxbetuQBWzoZPyQNhmDe0 jSGzNYoYFkjXkJcED1nrp3aV43MbXhBjknl60cHD+QRhSbtYZM8Zqnbc8KgRy2zx1MgZ lr7DRT5D/slp2Go1h0qmaAd99u62BYGLt+lpQlKyXcVO9Mdz6T0qI5XMR5grABtQU6gc St2g== X-Gm-Message-State: AOAM5322rqMfGFrIaa2A8dRbxVJ2T5BLWSzsn4RvWx5Hxv7pQa/XQc4B 6b2r5Std7iDZYYXNdmAl9gKaMQ== X-Google-Smtp-Source: ABdhPJzeObqQN5rRkuOn4D8ImXTgx9DiSKQAKO1voiymQblIg3tyOfyR3HekHOLuDNVQp1EIzny9wA== X-Received: by 2002:a7b:c303:: with SMTP id k3mr9048853wmj.44.1635022325405; Sat, 23 Oct 2021 13:52:05 -0700 (PDT) Received: from localhost.localdomain ([149.86.74.50]) by smtp.gmail.com with ESMTPSA id u16sm13555398wmc.21.2021.10.23.13.52.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Oct 2021 13:52:04 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet Subject: [PATCH bpf-next v2 5/5] bpftool: Switch to libbpf's hashmap for PIDs/names references Date: Sat, 23 Oct 2021 21:51:54 +0100 Message-Id: <20211023205154.6710-6-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211023205154.6710-1-quentin@isovalent.com> References: <20211023205154.6710-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net In order to show PIDs and names for processes holding references to BPF programs, maps, links, or BTF objects, bpftool creates hash maps to store all relevant information. This commit is part of a set that transitions from the kernel's hash map implementation to the one coming with libbpf. The motivation is to make bpftool less dependent of kernel headers, to ease the path to a potential out-of-tree mirror, like libbpf has. This is the third and final step of the transition, in which we convert the hash maps used for storing the information about the processes holding references to BPF objects (programs, maps, links, BTF), and at last we drop the inclusion of tools/include/linux/hashtable.h. Note: Checkpatch complains about the use of __weak declarations, and the missing empty lines after the bunch of empty function declarations when compiling without the BPF skeletons (none of these were introduced in this patch). We want to keep things as they are, and the reports should be safe to ignore. Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/btf.c | 7 ++-- tools/bpf/bpftool/link.c | 6 +-- tools/bpf/bpftool/main.c | 5 ++- tools/bpf/bpftool/main.h | 17 +++----- tools/bpf/bpftool/map.c | 6 +-- tools/bpf/bpftool/pids.c | 90 +++++++++++++++++++++++----------------- tools/bpf/bpftool/prog.c | 6 +-- 7 files changed, 72 insertions(+), 65 deletions(-) diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c index 11dd31b6e730..015d2758f826 100644 --- a/tools/bpf/bpftool/btf.c +++ b/tools/bpf/bpftool/btf.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include @@ -797,7 +796,7 @@ show_btf_plain(struct bpf_btf_info *info, int fd, hash_field_as_u32(entry->value)); } - emit_obj_refs_plain(&refs_table, info->id, "\n\tpids "); + emit_obj_refs_plain(refs_table, info->id, "\n\tpids "); printf("\n"); } @@ -830,7 +829,7 @@ show_btf_json(struct bpf_btf_info *info, int fd, } jsonw_end_array(json_wtr); /* map_ids */ - emit_obj_refs_json(&refs_table, info->id, json_wtr); /* pids */ + emit_obj_refs_json(refs_table, info->id, json_wtr); /* pids */ jsonw_bool_field(json_wtr, "kernel", info->kernel_btf); @@ -961,7 +960,7 @@ static int do_show(int argc, char **argv) exit_free: hashmap__free(btf_prog_table); hashmap__free(btf_map_table); - delete_obj_refs_table(&refs_table); + delete_obj_refs_table(refs_table); return err; } diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 404cc5459c6b..2c258db0d352 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -170,7 +170,7 @@ static int show_link_close_json(int fd, struct bpf_link_info *info) jsonw_end_array(json_wtr); } - emit_obj_refs_json(&refs_table, info->id, json_wtr); + emit_obj_refs_json(refs_table, info->id, json_wtr); jsonw_end_object(json_wtr); @@ -253,7 +253,7 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info) u32_as_hash_field(info->id)) printf("\n\tpinned %s", (char *)entry->value); } - emit_obj_refs_plain(&refs_table, info->id, "\n\tpids "); + emit_obj_refs_plain(refs_table, info->id, "\n\tpids "); printf("\n"); @@ -352,7 +352,7 @@ static int do_show(int argc, char **argv) if (json_output) jsonw_end_array(json_wtr); - delete_obj_refs_table(&refs_table); + delete_obj_refs_table(refs_table); if (show_pinned) delete_pinned_obj_table(link_table); diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c index 7a33f0e6da28..28237d7cef67 100644 --- a/tools/bpf/bpftool/main.c +++ b/tools/bpf/bpftool/main.c @@ -10,8 +10,9 @@ #include #include -#include #include +#include +#include #include "main.h" @@ -31,7 +32,7 @@ bool verifier_logs; bool relaxed_maps; bool use_loader; struct btf *base_btf; -struct obj_refs_table refs_table; +struct hashmap *refs_table; static void __noreturn clean_and_exit(int i) { diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index f6f2f951b193..383835c2604d 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -11,7 +11,6 @@ #include #include #include -#include #include #include @@ -92,7 +91,7 @@ extern bool verifier_logs; extern bool relaxed_maps; extern bool use_loader; extern struct btf *base_btf; -extern struct obj_refs_table refs_table; +extern struct hashmap *refs_table; void __printf(1, 2) p_err(const char *fmt, ...); void __printf(1, 2) p_info(const char *fmt, ...); @@ -106,18 +105,12 @@ void set_max_rlimit(void); int mount_tracefs(const char *target); -struct obj_refs_table { - DECLARE_HASHTABLE(table, 16); -}; - struct obj_ref { int pid; char comm[16]; }; struct obj_refs { - struct hlist_node node; - __u32 id; int ref_cnt; struct obj_ref *refs; }; @@ -128,12 +121,12 @@ struct bpf_line_info; int build_pinned_obj_table(struct hashmap *table, enum bpf_obj_type type); void delete_pinned_obj_table(struct hashmap *table); -__weak int build_obj_refs_table(struct obj_refs_table *table, +__weak int build_obj_refs_table(struct hashmap **table, enum bpf_obj_type type); -__weak void delete_obj_refs_table(struct obj_refs_table *table); -__weak void emit_obj_refs_json(struct obj_refs_table *table, __u32 id, +__weak void delete_obj_refs_table(struct hashmap *table); +__weak void emit_obj_refs_json(struct hashmap *table, __u32 id, json_writer_t *json_wtr); -__weak void emit_obj_refs_plain(struct obj_refs_table *table, __u32 id, +__weak void emit_obj_refs_plain(struct hashmap *table, __u32 id, const char *prefix); void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode); void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode); diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 2647603c5e5d..cae1f1119296 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -549,7 +549,7 @@ static int show_map_close_json(int fd, struct bpf_map_info *info) jsonw_end_array(json_wtr); } - emit_obj_refs_json(&refs_table, info->id, json_wtr); + emit_obj_refs_json(refs_table, info->id, json_wtr); jsonw_end_object(json_wtr); @@ -637,7 +637,7 @@ static int show_map_close_plain(int fd, struct bpf_map_info *info) if (frozen) printf("%sfrozen", info->btf_id ? " " : ""); - emit_obj_refs_plain(&refs_table, info->id, "\n\tpids "); + emit_obj_refs_plain(refs_table, info->id, "\n\tpids "); printf("\n"); return 0; @@ -748,7 +748,7 @@ static int do_show(int argc, char **argv) if (json_output) jsonw_end_array(json_wtr); - delete_obj_refs_table(&refs_table); + delete_obj_refs_table(refs_table); if (show_pinned) delete_pinned_obj_table(map_table); diff --git a/tools/bpf/bpftool/pids.c b/tools/bpf/bpftool/pids.c index 477e55d59c34..56b598eee043 100644 --- a/tools/bpf/bpftool/pids.c +++ b/tools/bpf/bpftool/pids.c @@ -6,35 +6,37 @@ #include #include #include + #include +#include #include "main.h" #include "skeleton/pid_iter.h" #ifdef BPFTOOL_WITHOUT_SKELETONS -int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type) +int build_obj_refs_table(struct hashmap **map, enum bpf_obj_type type) { return -ENOTSUP; } -void delete_obj_refs_table(struct obj_refs_table *table) {} -void emit_obj_refs_plain(struct obj_refs_table *table, __u32 id, const char *prefix) {} -void emit_obj_refs_json(struct obj_refs_table *table, __u32 id, json_writer_t *json_writer) {} +void delete_obj_refs_table(struct hashmap *map) {} +void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix) {} +void emit_obj_refs_json(struct hashmap *map, __u32 id, json_writer_t *json_writer) {} #else /* BPFTOOL_WITHOUT_SKELETONS */ #include "pid_iter.skel.h" -static void add_ref(struct obj_refs_table *table, struct pid_iter_entry *e) +static void add_ref(struct hashmap *map, struct pid_iter_entry *e) { + struct hashmap_entry *entry; struct obj_refs *refs; struct obj_ref *ref; + int err, i; void *tmp; - int i; - hash_for_each_possible(table->table, refs, node, e->id) { - if (refs->id != e->id) - continue; + hashmap__for_each_key_entry(map, entry, u32_as_hash_field(e->id)) { + refs = entry->value; for (i = 0; i < refs->ref_cnt; i++) { if (refs->refs[i].pid == e->pid) @@ -64,7 +66,6 @@ static void add_ref(struct obj_refs_table *table, struct pid_iter_entry *e) return; } - refs->id = e->id; refs->refs = malloc(sizeof(*refs->refs)); if (!refs->refs) { free(refs); @@ -76,7 +77,11 @@ static void add_ref(struct obj_refs_table *table, struct pid_iter_entry *e) ref->pid = e->pid; memcpy(ref->comm, e->comm, sizeof(ref->comm)); refs->ref_cnt = 1; - hash_add(table->table, &refs->node, e->id); + + err = hashmap__append(map, u32_as_hash_field(e->id), refs); + if (err) + p_err("failed to append entry to hashmap for ID %u: %s", + e->id, strerror(errno)); } static int __printf(2, 0) @@ -87,7 +92,7 @@ libbpf_print_none(__maybe_unused enum libbpf_print_level level, return 0; } -int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type) +int build_obj_refs_table(struct hashmap **map, enum bpf_obj_type type) { struct pid_iter_entry *e; char buf[4096 / sizeof(*e) * sizeof(*e)]; @@ -95,7 +100,11 @@ int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type) int err, ret, fd = -1, i; libbpf_print_fn_t default_print; - hash_init(table->table); + *map = hashmap__new(hash_fn_for_key_as_id, equal_fn_for_key_as_id, NULL); + if (!*map) { + p_err("failed to create hashmap for PID references"); + return -1; + } set_max_rlimit(); skel = pid_iter_bpf__open(); @@ -151,7 +160,7 @@ int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type) e = (void *)buf; for (i = 0; i < ret; i++, e++) { - add_ref(table, e); + add_ref(*map, e); } } err = 0; @@ -162,39 +171,44 @@ int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type) return err; } -void delete_obj_refs_table(struct obj_refs_table *table) +void delete_obj_refs_table(struct hashmap *map) { - struct obj_refs *refs; - struct hlist_node *tmp; - unsigned int bkt; + struct hashmap_entry *entry; + size_t bkt; + + if (!map) + return; + + hashmap__for_each_entry(map, entry, bkt) { + struct obj_refs *refs = entry->value; - hash_for_each_safe(table->table, bkt, tmp, refs, node) { - hash_del(&refs->node); free(refs->refs); free(refs); } + + hashmap__free(map); } -void emit_obj_refs_json(struct obj_refs_table *table, __u32 id, +void emit_obj_refs_json(struct hashmap *map, __u32 id, json_writer_t *json_writer) { - struct obj_refs *refs; - struct obj_ref *ref; - int i; + struct hashmap_entry *entry; - if (hash_empty(table->table)) + if (hashmap__empty(map)) return; - hash_for_each_possible(table->table, refs, node, id) { - if (refs->id != id) - continue; + hashmap__for_each_key_entry(map, entry, u32_as_hash_field(id)) { + struct obj_refs *refs = entry->value; + int i; + if (refs->ref_cnt == 0) break; jsonw_name(json_writer, "pids"); jsonw_start_array(json_writer); for (i = 0; i < refs->ref_cnt; i++) { - ref = &refs->refs[i]; + struct obj_ref *ref = &refs->refs[i]; + jsonw_start_object(json_writer); jsonw_int_field(json_writer, "pid", ref->pid); jsonw_string_field(json_writer, "comm", ref->comm); @@ -205,24 +219,24 @@ void emit_obj_refs_json(struct obj_refs_table *table, __u32 id, } } -void emit_obj_refs_plain(struct obj_refs_table *table, __u32 id, const char *prefix) +void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix) { - struct obj_refs *refs; - struct obj_ref *ref; - int i; + struct hashmap_entry *entry; - if (hash_empty(table->table)) + if (hashmap__empty(map)) return; - hash_for_each_possible(table->table, refs, node, id) { - if (refs->id != id) - continue; + hashmap__for_each_key_entry(map, entry, u32_as_hash_field(id)) { + struct obj_refs *refs = entry->value; + int i; + if (refs->ref_cnt == 0) break; printf("%s", prefix); for (i = 0; i < refs->ref_cnt; i++) { - ref = &refs->refs[i]; + struct obj_ref *ref = &refs->refs[i]; + printf("%s%s(%d)", i == 0 ? "" : ", ", ref->comm, ref->pid); } break; diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index 8fce78ce6f8b..515d22952602 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -430,7 +430,7 @@ static void print_prog_json(struct bpf_prog_info *info, int fd) jsonw_end_array(json_wtr); } - emit_obj_refs_json(&refs_table, info->id, json_wtr); + emit_obj_refs_json(refs_table, info->id, json_wtr); show_prog_metadata(fd, info->nr_map_ids); @@ -501,7 +501,7 @@ static void print_prog_plain(struct bpf_prog_info *info, int fd) if (info->btf_id) printf("\n\tbtf_id %d", info->btf_id); - emit_obj_refs_plain(&refs_table, info->id, "\n\tpids "); + emit_obj_refs_plain(refs_table, info->id, "\n\tpids "); printf("\n"); @@ -619,7 +619,7 @@ static int do_show(int argc, char **argv) if (json_output) jsonw_end_array(json_wtr); - delete_obj_refs_table(&refs_table); + delete_obj_refs_table(refs_table); if (show_pinned) delete_pinned_obj_table(prog_table);