public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Donglin Peng <dolinux.peng@gmail.com>
To: ast@kernel.org
Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
	Donglin Peng <dolinux.peng@gmail.com>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	Alan Maguire <alan.maguire@oracle.com>,
	Song Liu <song@kernel.org>, pengdonglin <pengdonglin@xiaomi.com>
Subject: [RFC PATCH v4 7/7] selftests/bpf: Add test cases for btf__permute functionality
Date: Tue,  4 Nov 2025 21:40:33 +0800	[thread overview]
Message-ID: <20251104134033.344807-8-dolinux.peng@gmail.com> (raw)
In-Reply-To: <20251104134033.344807-1-dolinux.peng@gmail.com>

From: pengdonglin <pengdonglin@xiaomi.com>

This patch introduces test cases for the btf__permute function to ensure
it works correctly with both base BTF and split BTF scenarios.

The test suite includes:
- test_permute_base: Validates permutation on standalone BTF
- test_permute_split: Tests permutation on split BTF with base dependencies

Each test verifies that type IDs are correctly rearranged and type
references are properly updated after permutation operations.

Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Alan Maguire <alan.maguire@oracle.com>
Cc: Song Liu <song@kernel.org>
Signed-off-by: pengdonglin <pengdonglin@xiaomi.com>
Signed-off-by: Donglin Peng <dolinux.peng@gmail.com>
---
 .../selftests/bpf/prog_tests/btf_permute.c    | 142 ++++++++++++++++++
 1 file changed, 142 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_permute.c

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_permute.c b/tools/testing/selftests/bpf/prog_tests/btf_permute.c
new file mode 100644
index 000000000000..2692cef627ab
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/btf_permute.c
@@ -0,0 +1,142 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Xiaomi */
+
+#include <test_progs.h>
+#include <bpf/btf.h>
+#include "btf_helpers.h"
+
+/* ensure btf__permute work as expected with base_btf */
+static void test_permute_base(void)
+{
+	struct btf *btf;
+	__u32 permute_ids[6];
+	int err;
+
+	btf = btf__new_empty();
+	if (!ASSERT_OK_PTR(btf, "empty_main_btf"))
+		return;
+
+	btf__add_int(btf, "int", 4, BTF_INT_SIGNED);	/* [1] int */
+	btf__add_ptr(btf, 1);				/* [2] ptr to int */
+	btf__add_struct(btf, "s1", 4);			/* [3] struct s1 { */
+	btf__add_field(btf, "m", 1, 0, 0);		/*       int m; */
+							/* } */
+	btf__add_struct(btf, "s2", 4);			/* [4] struct s2 { */
+	btf__add_field(btf, "m", 1, 0, 0);		/*       int m; */
+							/* } */
+	btf__add_func_proto(btf, 1);			/* [5] int (*)(int *p); */
+	btf__add_func_param(btf, "p", 2);
+	btf__add_func(btf, "f", BTF_FUNC_STATIC, 5);	/* [6] int f(int *p); */
+
+	VALIDATE_RAW_BTF(
+		btf,
+		"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+		"[2] PTR '(anon)' type_id=1",
+		"[3] STRUCT 's1' size=4 vlen=1\n"
+		"\t'm' type_id=1 bits_offset=0",
+		"[4] STRUCT 's2' size=4 vlen=1\n"
+		"\t'm' type_id=1 bits_offset=0",
+		"[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n"
+		"\t'p' type_id=2",
+		"[6] FUNC 'f' type_id=5 linkage=static");
+
+	permute_ids[0] = 4; /* struct s2 */
+	permute_ids[1] = 3; /* struct s1 */
+	permute_ids[2] = 5; /* int (*)(int *p) */
+	permute_ids[3] = 1; /* int */
+	permute_ids[4] = 6; /* int f(int *p) */
+	permute_ids[5] = 2; /* ptr to int */
+	err = btf__permute(btf, permute_ids, NULL);
+	if (!ASSERT_OK(err, "btf__permute"))
+		goto done;
+
+	VALIDATE_RAW_BTF(
+		btf,
+		"[1] STRUCT 's2' size=4 vlen=1\n"
+		"\t'm' type_id=4 bits_offset=0",
+		"[2] STRUCT 's1' size=4 vlen=1\n"
+		"\t'm' type_id=4 bits_offset=0",
+		"[3] FUNC_PROTO '(anon)' ret_type_id=4 vlen=1\n"
+		"\t'p' type_id=6",
+		"[4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+		"[5] FUNC 'f' type_id=3 linkage=static",
+		"[6] PTR '(anon)' type_id=4");
+
+done:
+	btf__free(btf);
+}
+
+/* ensure btf__permute work as expected with split_btf */
+static void test_permute_split(void)
+{
+	struct btf *split_btf = NULL, *base_btf = NULL;
+	__u32 permute_ids[4];
+	int err;
+
+	base_btf = btf__new_empty();
+	if (!ASSERT_OK_PTR(base_btf, "empty_main_btf"))
+		return;
+
+	btf__add_int(base_btf, "int", 4, BTF_INT_SIGNED);	/* [1] int */
+	btf__add_ptr(base_btf, 1);				/* [2] ptr to int */
+	VALIDATE_RAW_BTF(
+		base_btf,
+		"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+		"[2] PTR '(anon)' type_id=1");
+	split_btf = btf__new_empty_split(base_btf);
+	if (!ASSERT_OK_PTR(split_btf, "empty_split_btf"))
+		goto cleanup;
+	btf__add_struct(split_btf, "s1", 4);			/* [3] struct s1 { */
+	btf__add_field(split_btf, "m", 1, 0, 0);		/*   int m; */
+								/* } */
+	btf__add_struct(split_btf, "s2", 4);			/* [4] struct s2 { */
+	btf__add_field(split_btf, "m", 1, 0, 0);		/*   int m; */
+								/* } */
+	btf__add_func_proto(split_btf, 1);			/* [5] int (*)(int p); */
+	btf__add_func_param(split_btf, "p", 2);
+	btf__add_func(split_btf, "f", BTF_FUNC_STATIC, 5);	/* [6] int f(int *p); */
+
+	VALIDATE_RAW_BTF(
+		split_btf,
+		"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+		"[2] PTR '(anon)' type_id=1",
+		"[3] STRUCT 's1' size=4 vlen=1\n"
+		"\t'm' type_id=1 bits_offset=0",
+		"[4] STRUCT 's2' size=4 vlen=1\n"
+		"\t'm' type_id=1 bits_offset=0",
+		"[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n"
+		"\t'p' type_id=2",
+		"[6] FUNC 'f' type_id=5 linkage=static");
+
+	permute_ids[0] = 6; /* int f(int *p) */
+	permute_ids[1] = 3; /* struct s1 */
+	permute_ids[2] = 5; /* int (*)(int *p) */
+	permute_ids[3] = 4; /* struct s2 */
+	err = btf__permute(split_btf, permute_ids, NULL);
+	if (!ASSERT_OK(err, "btf__permute"))
+		goto cleanup;
+
+	VALIDATE_RAW_BTF(
+		split_btf,
+		"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
+		"[2] PTR '(anon)' type_id=1",
+		"[3] FUNC 'f' type_id=5 linkage=static",
+		"[4] STRUCT 's1' size=4 vlen=1\n"
+		"\t'm' type_id=1 bits_offset=0",
+		"[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n"
+		"\t'p' type_id=2",
+		"[6] STRUCT 's2' size=4 vlen=1\n"
+		"\t'm' type_id=1 bits_offset=0");
+
+cleanup:
+	btf__free(split_btf);
+	btf__free(base_btf);
+}
+
+void test_btf_permute(void)
+{
+	if (test__start_subtest("permute_base"))
+		test_permute_base();
+	if (test__start_subtest("permute_split"))
+		test_permute_split();
+}
-- 
2.34.1


  parent reply	other threads:[~2025-11-04 13:41 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-04 13:40 [RFC PATCH v4 0/7] libbpf: BTF performance optimizations with permutation and binary search Donglin Peng
2025-11-04 13:40 ` [RFC PATCH v4 1/7] libbpf: Extract BTF type remapping logic into helper function Donglin Peng
2025-11-04 23:16   ` Eduard Zingerman
2025-11-05  0:11   ` Andrii Nakryiko
2025-11-05  0:36     ` Eduard Zingerman
2025-11-05  0:57       ` Andrii Nakryiko
2025-11-05  1:23         ` Eduard Zingerman
2025-11-05 18:20           ` Andrii Nakryiko
2025-11-05 19:41             ` Eduard Zingerman
2025-11-06 17:09               ` Andrii Nakryiko
2025-11-04 13:40 ` [RFC PATCH v4 2/7] libbpf: Add BTF permutation support for type reordering Donglin Peng
2025-11-04 23:45   ` Eduard Zingerman
2025-11-05 11:31     ` Donglin Peng
2025-11-05  0:11   ` Andrii Nakryiko
2025-11-05  0:16     ` Eduard Zingerman
2025-11-05  1:04       ` Andrii Nakryiko
2025-11-05  1:20         ` Eduard Zingerman
2025-11-05 13:19           ` Donglin Peng
2025-11-05 18:32             ` Andrii Nakryiko
2025-11-05 18:23           ` Andrii Nakryiko
2025-11-05 19:23             ` Eduard Zingerman
2025-11-06 17:21               ` Andrii Nakryiko
2025-11-07  2:36             ` Donglin Peng
2025-11-07 17:43               ` Andrii Nakryiko
2025-11-05 12:52     ` Donglin Peng
2025-11-05 18:29       ` Andrii Nakryiko
2025-11-06  7:31         ` Donglin Peng
2025-11-06 17:12           ` Andrii Nakryiko
2025-11-07  1:39             ` Donglin Peng
2025-11-04 13:40 ` [RFC PATCH v4 3/7] libbpf: Optimize type lookup with binary search for sorted BTF Donglin Peng
2025-11-04 14:15   ` bot+bpf-ci
2025-11-05  0:06   ` Eduard Zingerman
2025-11-05  0:11   ` Andrii Nakryiko
2025-11-05  0:19     ` Eduard Zingerman
2025-11-05  0:54       ` Andrii Nakryiko
2025-11-05  1:17         ` Eduard Zingerman
2025-11-05 13:48           ` Donglin Peng
2025-11-05 16:52             ` Eduard Zingerman
2025-11-06  6:10               ` Donglin Peng
2025-11-05 18:11             ` Andrii Nakryiko
2025-11-06  7:49               ` Donglin Peng
2025-11-06 17:31                 ` Andrii Nakryiko
2025-11-07  4:57                   ` Donglin Peng
2025-11-07 17:01                     ` Andrii Nakryiko
2025-11-10  2:04                       ` Donglin Peng
2025-11-04 13:40 ` [RFC PATCH v4 4/7] libbpf: Implement lazy sorting validation for binary search optimization Donglin Peng
2025-11-05  0:29   ` Eduard Zingerman
2025-11-04 13:40 ` [RFC PATCH v4 5/7] btf: Optimize type lookup with binary search Donglin Peng
2025-11-04 17:14   ` Alexei Starovoitov
2025-11-05 13:22     ` Donglin Peng
2025-11-04 13:40 ` [RFC PATCH v4 6/7] btf: Add lazy sorting validation for " Donglin Peng
2025-11-04 13:40 ` Donglin Peng [this message]
2025-11-05  0:41   ` [RFC PATCH v4 7/7] selftests/bpf: Add test cases for btf__permute functionality Eduard Zingerman

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20251104134033.344807-8-dolinux.peng@gmail.com \
    --to=dolinux.peng@gmail.com \
    --cc=alan.maguire@oracle.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=eddyz87@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pengdonglin@xiaomi.com \
    --cc=song@kernel.org \
    /path/to/YOUR_REPLY

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

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