public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 bpf-next 0/2] Multi-split BTF fixes and test
@ 2025-11-04 20:33 Alan Maguire
  2025-11-04 20:33 ` [PATCH v4 bpf-next 1/2] libbpf: Fix parsing of multi-split BTF Alan Maguire
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Alan Maguire @ 2025-11-04 20:33 UTC (permalink / raw)
  To: andrii
  Cc: eddyz87, ast, daniel, martin.lau, yonghong.song, song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, ihor.solodrai, bpf,
	Alan Maguire

This small series consists of a fix to multi-split BTF parsing
(patch 1) and a test which exercises (multi-)split BTF parsing
(patch 2).

Changes since v3 [1]
- add asserts to ensure number of types in original and parsed
  BTF are identical, and the calls to btf__type_by_id() return
  valid pointers (code review bot, patch 2)

Changes since v2 [2]

- fix Fixes: tag formatting (Andrii, patch 1)
- BPF code-review bot saw we were doing ASSERT_OK_PTR() on wrong
  BTF (not multisplit) in patch 2
- ensure cleanup is correctly handled for BTF, unlink in split
  tests (Andrii, patch 2)

Changes since v1 [3]

- BPF code-review bot spotted another place that the string offset
needed to be adjusted based upon base start string offset + header
string offset.
- added selftests to extend split BTF testing to parsing

[1] https://lore.kernel.org/bpf/20251028225544.1312356-1-alan.maguire@oracle.com/
[2] https://lore.kernel.org/bpf/20251028155709.1265445-1-alan.maguire@oracle.com/
[3] https://lore.kernel.org/bpf/20251023142812.258870-1-alan.maguire@oracle.com/

Alan Maguire (2):
  libbpf: Fix parsing of multi-split BTF
  selftests/bpf: Test parsing of (multi-)split BTF

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

-- 
2.39.3


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

* [PATCH v4 bpf-next 1/2] libbpf: Fix parsing of multi-split BTF
  2025-11-04 20:33 [PATCH v4 bpf-next 0/2] Multi-split BTF fixes and test Alan Maguire
@ 2025-11-04 20:33 ` Alan Maguire
  2025-11-04 20:33 ` [PATCH v4 bpf-next 2/2] selftests/bpf: Test parsing of (multi-)split BTF Alan Maguire
  2025-11-04 22:30 ` [PATCH v4 bpf-next 0/2] Multi-split BTF fixes and test patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Alan Maguire @ 2025-11-04 20:33 UTC (permalink / raw)
  To: andrii
  Cc: eddyz87, ast, daniel, martin.lau, yonghong.song, song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, ihor.solodrai, bpf,
	Alan Maguire

When creating multi-split BTF we correctly set the start string offset
to be the size of the base string section plus the base BTF start
string offset; the latter is needed for multi-split BTF since the
offset is non-zero there.

Unfortunately the BTF parsing case needed that logic and it was
missed.

Fixes: 4e29128a9ace ("libbpf/btf: Fix string handling to support multi-split BTF")
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
 tools/lib/bpf/btf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 18907f0fcf9f..9f141395c074 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -1061,7 +1061,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf, b
 	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;
 	}
 
 	if (is_mmap) {
@@ -5818,7 +5818,7 @@ void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
 {
 	btf->base_btf = (struct 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;
 }
 
 int btf__relocate(struct btf *btf, const struct btf *base_btf)
-- 
2.39.3


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

* [PATCH v4 bpf-next 2/2] selftests/bpf: Test parsing of (multi-)split BTF
  2025-11-04 20:33 [PATCH v4 bpf-next 0/2] Multi-split BTF fixes and test Alan Maguire
  2025-11-04 20:33 ` [PATCH v4 bpf-next 1/2] libbpf: Fix parsing of multi-split BTF Alan Maguire
@ 2025-11-04 20:33 ` Alan Maguire
  2025-11-04 22:30 ` [PATCH v4 bpf-next 0/2] Multi-split BTF fixes and test patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Alan Maguire @ 2025-11-04 20:33 UTC (permalink / raw)
  To: andrii
  Cc: eddyz87, ast, daniel, martin.lau, yonghong.song, song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, ihor.solodrai, bpf,
	Alan Maguire

Write raw BTF to files, parse it and compare to original;
this allows us to test parsing of (multi-)split BTF code.

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

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_split.c b/tools/testing/selftests/bpf/prog_tests/btf_split.c
index 3696fb9a05ed..2d47cad50a51 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_split.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_split.c
@@ -12,11 +12,45 @@ static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
 	vfprintf(ctx, fmt, args);
 }
 
+/* Write raw BTF to file, return number of bytes written or negative errno */
+static ssize_t btf_raw_write(struct btf *btf, char *file)
+{
+	ssize_t written = 0;
+	const void *data;
+	__u32 size = 0;
+	int fd, ret;
+
+	fd = mkstemp(file);
+	if (!ASSERT_GE(fd, 0, "create_file"))
+		return -errno;
+
+	data = btf__raw_data(btf, &size);
+	if (!ASSERT_OK_PTR(data, "btf__raw_data")) {
+		close(fd);
+		return -EINVAL;
+	}
+	while (written < size) {
+		ret = write(fd, data + written, size - written);
+		if (!ASSERT_GE(ret, 0, "write succeeded")) {
+			close(fd);
+			return -errno;
+		}
+		written += ret;
+	}
+	close(fd);
+	return written;
+}
+
 static void __test_btf_split(bool multi)
 {
+	char multisplit_btf_file[] = "/tmp/test_btf_multisplit.XXXXXX";
+	char split_btf_file[] = "/tmp/test_btf_split.XXXXXX";
+	char base_btf_file[] = "/tmp/test_btf_base.XXXXXX";
+	ssize_t multisplit_btf_sz = 0, split_btf_sz = 0, base_btf_sz = 0;
 	struct btf_dump *d = NULL;
-	const struct btf_type *t;
-	struct btf *btf1, *btf2, *btf3 = NULL;
+	const struct btf_type *t, *ot;
+	struct btf *btf1 = NULL, *btf2 = NULL, *btf3 = NULL;
+	struct btf *btf4 = NULL, *btf5 = NULL, *btf6 = NULL;
 	int str_off, i, err;
 
 	btf1 = btf__new_empty();
@@ -123,6 +157,45 @@ static void __test_btf_split(bool multi)
 "	int uf2;\n"
 "};\n\n", "c_dump");
 
+	/* write base, split BTFs to files and ensure parsing succeeds */
+	base_btf_sz = btf_raw_write(btf1, base_btf_file);
+	if (base_btf_sz < 0)
+		goto cleanup;
+	split_btf_sz = btf_raw_write(btf2, split_btf_file);
+	if (split_btf_sz < 0)
+		goto cleanup;
+	btf4 = btf__parse(base_btf_file, NULL);
+	if (!ASSERT_OK_PTR(btf4, "parse_base"))
+		goto cleanup;
+	btf5 = btf__parse_split(split_btf_file, btf4);
+	if (!ASSERT_OK_PTR(btf5, "parse_split"))
+		goto cleanup;
+	if (multi) {
+		multisplit_btf_sz = btf_raw_write(btf3, multisplit_btf_file);
+		if (multisplit_btf_sz < 0)
+			goto cleanup;
+		btf6 = btf__parse_split(multisplit_btf_file, btf5);
+		if (!ASSERT_OK_PTR(btf6, "parse_multisplit"))
+			goto cleanup;
+	} else {
+		btf6 = btf5;
+	}
+
+	if (!ASSERT_EQ(btf__type_cnt(btf3), btf__type_cnt(btf6), "cmp_type_cnt"))
+		goto cleanup;
+
+	/* compare parsed to original BTF */
+	for (i = 1; i < btf__type_cnt(btf6); i++) {
+		t = btf__type_by_id(btf6, i);
+		if (!ASSERT_OK_PTR(t, "type_in_parsed_btf"))
+			goto cleanup;
+		ot = btf__type_by_id(btf3, i);
+		if (!ASSERT_OK_PTR(ot, "type_in_orig_btf"))
+			goto cleanup;
+		if (!ASSERT_EQ(memcmp(t, ot, sizeof(*ot)), 0, "cmp_parsed_orig_btf"))
+			goto cleanup;
+	}
+
 cleanup:
 	if (dump_buf_file)
 		fclose(dump_buf_file);
@@ -132,6 +205,16 @@ static void __test_btf_split(bool multi)
 	btf__free(btf2);
 	if (btf2 != btf3)
 		btf__free(btf3);
+	btf__free(btf4);
+	btf__free(btf5);
+	if (btf5 != btf6)
+		btf__free(btf6);
+	if (base_btf_sz > 0)
+		unlink(base_btf_file);
+	if (split_btf_sz > 0)
+		unlink(split_btf_file);
+	if (multisplit_btf_sz > 0)
+		unlink(multisplit_btf_file);
 }
 
 void test_btf_split(void)
-- 
2.39.3


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

* Re: [PATCH v4 bpf-next 0/2] Multi-split BTF fixes and test
  2025-11-04 20:33 [PATCH v4 bpf-next 0/2] Multi-split BTF fixes and test Alan Maguire
  2025-11-04 20:33 ` [PATCH v4 bpf-next 1/2] libbpf: Fix parsing of multi-split BTF Alan Maguire
  2025-11-04 20:33 ` [PATCH v4 bpf-next 2/2] selftests/bpf: Test parsing of (multi-)split BTF Alan Maguire
@ 2025-11-04 22:30 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-11-04 22:30 UTC (permalink / raw)
  To: Alan Maguire
  Cc: andrii, eddyz87, ast, daniel, martin.lau, yonghong.song, song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, ihor.solodrai, bpf

Hello:

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

On Tue,  4 Nov 2025 20:33:07 +0000 you wrote:
> This small series consists of a fix to multi-split BTF parsing
> (patch 1) and a test which exercises (multi-)split BTF parsing
> (patch 2).
> 
> Changes since v3 [1]
> - add asserts to ensure number of types in original and parsed
>   BTF are identical, and the calls to btf__type_by_id() return
>   valid pointers (code review bot, patch 2)
> 
> [...]

Here is the summary with links:
  - [v4,bpf-next,1/2] libbpf: Fix parsing of multi-split BTF
    https://git.kernel.org/bpf/bpf-next/c/4f596acc260e
  - [v4,bpf-next,2/2] selftests/bpf: Test parsing of (multi-)split BTF
    https://git.kernel.org/bpf/bpf-next/c/cc77a203896e

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-11-04 22:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-04 20:33 [PATCH v4 bpf-next 0/2] Multi-split BTF fixes and test Alan Maguire
2025-11-04 20:33 ` [PATCH v4 bpf-next 1/2] libbpf: Fix parsing of multi-split BTF Alan Maguire
2025-11-04 20:33 ` [PATCH v4 bpf-next 2/2] selftests/bpf: Test parsing of (multi-)split BTF Alan Maguire
2025-11-04 22:30 ` [PATCH v4 bpf-next 0/2] Multi-split BTF fixes and test 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