bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/2] libbpf: support multi-split BTF
@ 2025-05-19 16:59 Alan Maguire
  2025-05-19 16:59 ` [PATCH bpf-next 1/2] libbpf/btf: Fix string handling to " Alan Maguire
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Alan Maguire @ 2025-05-19 16:59 UTC (permalink / raw)
  To: andrii
  Cc: ast, ttreyer, daniel, martin.lau, eddyz87, song, yonghong.song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, mykolal, bpf,
	Alan Maguire

In discussing handling of inlines in BTF [1], one area which we may need
support for in the future is multiple split BTF, where split BTF sits
atop another split BTF which sits atop base BTF.  This two-patch series
fixes one issue discovered when testing multi-split BTF and extends the
split BTF test to cover multi-split BTF also.

[1] https://lore.kernel.org/dwarves/20250416-btf_inline-v1-0-e4bd2f8adae5@meta.com/

Alan Maguire (2):
  libbpf/btf: Fix string handling to support multi-split BTF
  selftests/bpf: Test multi-split BTF

 tools/lib/bpf/btf.c                           |  2 +-
 .../selftests/bpf/prog_tests/btf_split.c      | 58 +++++++++++++++++--
 2 files changed, 53 insertions(+), 7 deletions(-)

-- 
2.39.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf-next 1/2] libbpf/btf: Fix string handling to support multi-split BTF
  2025-05-19 16:59 [PATCH bpf-next 0/2] libbpf: support multi-split BTF Alan Maguire
@ 2025-05-19 16:59 ` Alan Maguire
  2025-05-19 16:59 ` [PATCH bpf-next 2/2] selftests/bpf: Test " Alan Maguire
  2025-05-20 23:30 ` [PATCH bpf-next 0/2] libbpf: support " patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Alan Maguire @ 2025-05-19 16:59 UTC (permalink / raw)
  To: andrii
  Cc: ast, ttreyer, daniel, martin.lau, eddyz87, song, yonghong.song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, mykolal, bpf,
	Alan Maguire

libbpf handling of split BTF has been written largely with the
assumption that multiple splits are possible, i.e. split BTF on top of
split BTF on top of base BTF.  One area where this does not quite work
is string handling in split BTF; the start string offset should be the
base BTF string section length + the base BTF string offset.  This
worked in the past because for a single split BTF with base the start
string offset was always 0.

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
 tools/lib/bpf/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index f18d7e6a453c..8d0d0b645a75 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -996,7 +996,7 @@ static struct btf *btf_new_empty(struct btf *base_btf)
 	if (base_btf) {
 		btf->base_btf = base_btf;
 		btf->start_id = btf__type_cnt(base_btf);
-		btf->start_str_off = base_btf->hdr->str_len;
+		btf->start_str_off = base_btf->hdr->str_len + base_btf->start_str_off;
 		btf->swapped_endian = base_btf->swapped_endian;
 	}
 
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH bpf-next 2/2] selftests/bpf: Test multi-split BTF
  2025-05-19 16:59 [PATCH bpf-next 0/2] libbpf: support multi-split BTF Alan Maguire
  2025-05-19 16:59 ` [PATCH bpf-next 1/2] libbpf/btf: Fix string handling to " Alan Maguire
@ 2025-05-19 16:59 ` Alan Maguire
  2025-05-20 23:30 ` [PATCH bpf-next 0/2] libbpf: support " patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Alan Maguire @ 2025-05-19 16:59 UTC (permalink / raw)
  To: andrii
  Cc: ast, ttreyer, daniel, martin.lau, eddyz87, song, yonghong.song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, mykolal, bpf,
	Alan Maguire

Extend split BTF test to cover case where we create split BTF on top of
existing split BTF and add info to it; ensure that such BTF can be
created and handled by searching within it, dumping/comparing to expected.

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
 .../selftests/bpf/prog_tests/btf_split.c      | 58 +++++++++++++++++--
 1 file changed, 52 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_split.c b/tools/testing/selftests/bpf/prog_tests/btf_split.c
index eef1158676ed..3696fb9a05ed 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_split.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_split.c
@@ -12,10 +12,11 @@ static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
 	vfprintf(ctx, fmt, args);
 }
 
-void test_btf_split() {
+static void __test_btf_split(bool multi)
+{
 	struct btf_dump *d = NULL;
 	const struct btf_type *t;
-	struct btf *btf1, *btf2;
+	struct btf *btf1, *btf2, *btf3 = NULL;
 	int str_off, i, err;
 
 	btf1 = btf__new_empty();
@@ -63,14 +64,46 @@ void test_btf_split() {
 	ASSERT_EQ(btf_vlen(t), 3, "split_struct_vlen");
 	ASSERT_STREQ(btf__str_by_offset(btf2, t->name_off), "s2", "split_struct_name");
 
+	if (multi) {
+		btf3 = btf__new_empty_split(btf2);
+		if (!ASSERT_OK_PTR(btf3, "multi_split_btf"))
+			goto cleanup;
+	} else {
+		btf3 = btf2;
+	}
+
+	btf__add_union(btf3, "u1", 16);			/* [5] union u1 {	*/
+	btf__add_field(btf3, "f1", 4, 0, 0);		/*	struct s2 f1;	*/
+	btf__add_field(btf3, "uf2", 1, 0, 0);		/*	int f2;		*/
+							/* } */
+
+	if (multi) {
+		t = btf__type_by_id(btf2, 5);
+		ASSERT_NULL(t, "multisplit_type_in_first_split");
+	}
+
+	t = btf__type_by_id(btf3, 5);
+	if (!ASSERT_OK_PTR(t, "split_union_type"))
+		goto cleanup;
+	ASSERT_EQ(btf_is_union(t), true, "split_union_kind");
+	ASSERT_EQ(btf_vlen(t), 2, "split_union_vlen");
+	ASSERT_STREQ(btf__str_by_offset(btf3, t->name_off), "u1", "split_union_name");
+	ASSERT_EQ(btf__type_cnt(btf3), 6, "split_type_cnt");
+
+	t = btf__type_by_id(btf3, 1);
+	if (!ASSERT_OK_PTR(t, "split_base_type"))
+		goto cleanup;
+	ASSERT_EQ(btf_is_int(t), true, "split_base_int");
+	ASSERT_STREQ(btf__str_by_offset(btf3, t->name_off), "int", "split_base_type_name");
+
 	/* BTF-to-C dump of split BTF */
 	dump_buf_file = open_memstream(&dump_buf, &dump_buf_sz);
 	if (!ASSERT_OK_PTR(dump_buf_file, "dump_memstream"))
 		return;
-	d = btf_dump__new(btf2, btf_dump_printf, dump_buf_file, NULL);
+	d = btf_dump__new(btf3, btf_dump_printf, dump_buf_file, NULL);
 	if (!ASSERT_OK_PTR(d, "btf_dump__new"))
 		goto cleanup;
-	for (i = 1; i < btf__type_cnt(btf2); i++) {
+	for (i = 1; i < btf__type_cnt(btf3); i++) {
 		err = btf_dump__dump_type(d, i);
 		ASSERT_OK(err, "dump_type_ok");
 	}
@@ -79,12 +112,15 @@ void test_btf_split() {
 	ASSERT_STREQ(dump_buf,
 "struct s1 {\n"
 "	int f1;\n"
-"};\n"
-"\n"
+"};\n\n"
 "struct s2 {\n"
 "	struct s1 f1;\n"
 "	int f2;\n"
 "	int *f3;\n"
+"};\n\n"
+"union u1 {\n"
+"	struct s2 f1;\n"
+"	int uf2;\n"
 "};\n\n", "c_dump");
 
 cleanup:
@@ -94,4 +130,14 @@ void test_btf_split() {
 	btf_dump__free(d);
 	btf__free(btf1);
 	btf__free(btf2);
+	if (btf2 != btf3)
+		btf__free(btf3);
+}
+
+void test_btf_split(void)
+{
+	if (test__start_subtest("single_split"))
+		__test_btf_split(false);
+	if (test__start_subtest("multi_split"))
+		__test_btf_split(true);
 }
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next 0/2] libbpf: support multi-split BTF
  2025-05-19 16:59 [PATCH bpf-next 0/2] libbpf: support multi-split BTF Alan Maguire
  2025-05-19 16:59 ` [PATCH bpf-next 1/2] libbpf/btf: Fix string handling to " Alan Maguire
  2025-05-19 16:59 ` [PATCH bpf-next 2/2] selftests/bpf: Test " Alan Maguire
@ 2025-05-20 23:30 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-05-20 23:30 UTC (permalink / raw)
  To: Alan Maguire
  Cc: andrii, ast, ttreyer, daniel, martin.lau, eddyz87, song,
	yonghong.song, john.fastabend, kpsingh, sdf, haoluo, jolsa,
	mykolal, bpf

Hello:

This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Mon, 19 May 2025 17:59:33 +0100 you wrote:
> In discussing handling of inlines in BTF [1], one area which we may need
> support for in the future is multiple split BTF, where split BTF sits
> atop another split BTF which sits atop base BTF.  This two-patch series
> fixes one issue discovered when testing multi-split BTF and extends the
> split BTF test to cover multi-split BTF also.
> 
> [1] https://lore.kernel.org/dwarves/20250416-btf_inline-v1-0-e4bd2f8adae5@meta.com/
> 
> [...]

Here is the summary with links:
  - [bpf-next,1/2] libbpf/btf: Fix string handling to support multi-split BTF
    https://git.kernel.org/bpf/bpf-next/c/4e29128a9ace
  - [bpf-next,2/2] selftests/bpf: Test multi-split BTF
    https://git.kernel.org/bpf/bpf-next/c/02f5e7c1f3ea

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-05-20 23:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-19 16:59 [PATCH bpf-next 0/2] libbpf: support multi-split BTF Alan Maguire
2025-05-19 16:59 ` [PATCH bpf-next 1/2] libbpf/btf: Fix string handling to " Alan Maguire
2025-05-19 16:59 ` [PATCH bpf-next 2/2] selftests/bpf: Test " Alan Maguire
2025-05-20 23:30 ` [PATCH bpf-next 0/2] libbpf: support " patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).