From patchwork Tue Feb 22 07:45:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Kuohai X-Patchwork-Id: 12754632 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 C1F10C43219 for ; Tue, 22 Feb 2022 07:41:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230116AbiBVHmB (ORCPT ); Tue, 22 Feb 2022 02:42:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229817AbiBVHl4 (ORCPT ); Tue, 22 Feb 2022 02:41:56 -0500 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33794D5F64 for ; Mon, 21 Feb 2022 23:34:46 -0800 (PST) Received: from kwepemi500013.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4K2rRW4Fp1z1FD8j; Tue, 22 Feb 2022 15:30:15 +0800 (CST) Received: from huawei.com (10.67.174.197) by kwepemi500013.china.huawei.com (7.221.188.120) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Tue, 22 Feb 2022 15:34:44 +0800 From: Xu Kuohai To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Yonghong Song , Shuah Khan , Martin KaFai Lau , Song Liu Subject: [PATCH bpf-next 1/2] libbpf: Skip BTF_KIND_FWD when counting duplicated type names Date: Tue, 22 Feb 2022 02:45:23 -0500 Message-ID: <20220222074524.1027060-2-xukuohai@huawei.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220222074524.1027060-1-xukuohai@huawei.com> References: <20220222074524.1027060-1-xukuohai@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.174.197] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemi500013.china.huawei.com (7.221.188.120) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net If a FWD appears in the BTF before a STRUCT with the same name, the STRUCT is dumped with a conflicted name: $ bpftool btf dump file vmlinux format raw | grep "'unix_sock'" [81287] FWD 'unix_sock' fwd_kind=struct [89336] STRUCT 'unix_sock' size=1024 vlen=14 $ bpftool btf dump file vmlinux format c | grep "struct unix_sock" struct unix_sock; struct unix_sock___2 { <--- conflict, the "___2" is unexpected struct unix_sock___2 *unix_sk; Fixes: 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion") Signed-off-by: Xu Kuohai Acked-by: Song Liu --- tools/lib/bpf/btf_dump.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c index 07ebe70d3a30..55079efbd8f1 100644 --- a/tools/lib/bpf/btf_dump.c +++ b/tools/lib/bpf/btf_dump.c @@ -1505,13 +1505,15 @@ static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id, if (s->name_resolved) return *cached_name ? *cached_name : orig_name; - dup_cnt = btf_dump_name_dups(d, name_map, orig_name); - if (dup_cnt > 1) { - const size_t max_len = 256; - char new_name[max_len]; - - snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt); - *cached_name = strdup(new_name); + if (!btf_is_fwd(t)) { + dup_cnt = btf_dump_name_dups(d, name_map, orig_name); + if (dup_cnt > 1) { + const size_t max_len = 256; + char new_name[max_len]; + + snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt); + *cached_name = strdup(new_name); + } } s->name_resolved = 1; From patchwork Tue Feb 22 07:45:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Kuohai X-Patchwork-Id: 12754631 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 6BFD0C43217 for ; Tue, 22 Feb 2022 07:41:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229632AbiBVHmA (ORCPT ); Tue, 22 Feb 2022 02:42:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229850AbiBVHl4 (ORCPT ); Tue, 22 Feb 2022 02:41:56 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 338E9D5F69 for ; Mon, 21 Feb 2022 23:34:47 -0800 (PST) Received: from kwepemi500013.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4K2rWL45p2zdZWs; Tue, 22 Feb 2022 15:33:34 +0800 (CST) Received: from huawei.com (10.67.174.197) by kwepemi500013.china.huawei.com (7.221.188.120) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Tue, 22 Feb 2022 15:34:44 +0800 From: Xu Kuohai To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Yonghong Song , Shuah Khan , Martin KaFai Lau , Song Liu Subject: [PATCH bpf-next 2/2] selftests/bpf: Update btf_dump case for conflict FWD and STRUCT name Date: Tue, 22 Feb 2022 02:45:24 -0500 Message-ID: <20220222074524.1027060-3-xukuohai@huawei.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220222074524.1027060-1-xukuohai@huawei.com> References: <20220222074524.1027060-1-xukuohai@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.174.197] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemi500013.china.huawei.com (7.221.188.120) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Update btf_dump test case for conflict FWD and STRUCT name. Signed-off-by: Xu Kuohai Acked-by: Song Liu --- tools/testing/selftests/bpf/prog_tests/btf_dump.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c index 9e26903f9170..2539a8f8b098 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c +++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c @@ -150,6 +150,8 @@ static void test_btf_dump_incremental(void) * * enum { VAL = 1 }; * + * struct s; + * * struct s { int x; }; * */ @@ -161,8 +163,11 @@ static void test_btf_dump_incremental(void) id = btf__add_int(btf, "int", 4, BTF_INT_SIGNED); ASSERT_EQ(id, 2, "int_id"); + id = btf__add_fwd(btf, "s", BTF_FWD_STRUCT); + ASSERT_EQ(id, 3, "fwd_id"); + id = btf__add_struct(btf, "s", 4); - ASSERT_EQ(id, 3, "struct_id"); + ASSERT_EQ(id, 4, "struct_id"); err = btf__add_field(btf, "x", 2, 0, 0); ASSERT_OK(err, "field_ok"); @@ -178,6 +183,8 @@ static void test_btf_dump_incremental(void) " VAL = 1,\n" "};\n" "\n" +"struct s;\n" +"\n" "struct s {\n" " int x;\n" "};\n\n", "c_dump1"); @@ -199,7 +206,7 @@ static void test_btf_dump_incremental(void) fseek(dump_buf_file, 0, SEEK_SET); id = btf__add_struct(btf, "s", 4); - ASSERT_EQ(id, 4, "struct_id"); + ASSERT_EQ(id, 5, "struct_id"); err = btf__add_field(btf, "x", 1, 0, 0); ASSERT_OK(err, "field_ok"); err = btf__add_field(btf, "s", 3, 32, 0);