Skip to contentSkip to conversation

bpf: fix a couple of missed btf_type_tag handling in libbpf

PATCH Applied Yonghong Song 3 participants 6 messages Nov 15, 2021 – Nov 16, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover bpf: fix a couple of missed btf_type_tag handling in libbpf

Commit 2dc1e488e5cd ("libbpf: Support BTF_KIND_TYPE_TAG") added BTF_KIND_TYPE_TAG support. But BTF_KIND_TYPE_TAG is not handled properly in libbpf btf_dedup_is_equiv() which will cause pahole dedup failure if the kernel has the following hack:
#define __user __attribute__((btf_type_tag("user")))

Patch 1 fixed the issue and Patch 2 added a test for it.

Yonghong Song (2):
libbpf: fix a couple of missed btf_type_tag handling in btf.c selftests/bpf: add a dedup selftest with equivalent structure types

 tools/lib/bpf/btf.c                          |  2 ++
 tools/testing/selftests/bpf/prog_tests/btf.c | 26 ++++++++++++++++++++
 2 files changed, 28 insertions(+)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/2 libbpf: fix a couple of missed btf_type_tag handling in btf.c

Commit 2dc1e488e5cd ("libbpf: Support BTF_KIND_TYPE_TAG") added BTF_KIND_TYPE_TAG support. But to test vmlinux build with
#define __user __attribute__((btf_type_tag("user")))
I need to sync libbpf repo and manually copy libbpf sources to pahole. To simplify process, I used BTF_KIND_RESTRICT to simulate BTF_KIND_TYPE_TAG with vmlinux build as "restrict" modifier is barely used in kernel. But this approach missed one case in dedup with structures where BTF_KIND_RESTRICT is handled and BTF_KIND_TYPE_TAG is not handled in btf_dedup_is_equiv(), and this will result in pahole dedup failure. This patch fixed this issue and a selftest is added in the subsequent patch to test this scenario.

The other missed handling is in btf__resolve_size(). Currently the compiler always emit like PTR->TYPE_TAG->... so in practice we don't hit the missing BTF_KIND_TYPE_TAG handling issue with compiler generated code. But let us added case BTF_KIND_TYPE_TAG in the switch statement
to be future proof.

Fixes 2dc1e488e5cd ("libbpf: Support BTF_KIND_TYPE_TAG")
---
 tools/lib/bpf/btf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/btf.c +2 -0
index fadf089ae8fe..b6be579e0dc6 100644
@@ -610,6 +610,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
 		case BTF_KIND_RESTRICT:
 		case BTF_KIND_VAR:
 		case BTF_KIND_DECL_TAG:
+		case BTF_KIND_TYPE_TAG:
 			type_id = t->type;
 			break;
 		case BTF_KIND_ARRAY:
@@ -4023,6 +4024,7 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
 	case BTF_KIND_PTR:
 	case BTF_KIND_TYPEDEF:
 	case BTF_KIND_FUNC:
+	case BTF_KIND_TYPE_TAG:
 		if (cand_type->info != canon_type->info)
 			return 0;
 		return btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/2 selftests/bpf: add a dedup selftest with equivalent structure types

In reply to Yonghong Song

Without previous libbpf patch, the following error will occur:

  $ ./test_progs -t btf
  ...
  do_test_dedup:FAIL:check btf_dedup failed errno:-22#13/205 btf/dedup: btf_type_tag #5, struct:FAIL

And the previfous libbpf patch fixed the issue.

---
 tools/testing/selftests/bpf/prog_tests/btf.c | 26 ++++++++++++++++++++
 1 file changed, 26 insertions(+)
tools/testing/selftests/bpf/prog_tests/btf.c +26 -0
index 4aa6343dc4c8..f9326a13badb 100644
@@ -7352,6 +7352,32 @@ static struct btf_dedup_test dedup_tests[] = {
 		BTF_STR_SEC("\0tag1"),
 	},
 },
+{
+	.descr = "dedup: btf_type_tag #5, struct",
+	.input = {
+		.raw_types = {
+			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),				/* [1] */
+			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),					/* [2] */
+			BTF_TYPE_ENC(NAME_NTH(2), BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 4),	/* [3] */
+			BTF_MEMBER_ENC(NAME_NTH(3), 2, BTF_MEMBER_OFFSET(0, 0)),
+			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),					/* [4] */
+			BTF_TYPE_ENC(NAME_NTH(2), BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 4),	/* [5] */
+			BTF_MEMBER_ENC(NAME_NTH(3), 4, BTF_MEMBER_OFFSET(0, 0)),
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0tag1\0t\0m"),
+	},
+	.expect = {
+		.raw_types = {
+			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),				/* [1] */
+			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),					/* [2] */
+			BTF_TYPE_ENC(NAME_NTH(2), BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 4),	/* [3] */
+			BTF_MEMBER_ENC(NAME_NTH(3), 2, BTF_MEMBER_OFFSET(0, 0)),
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0tag1\0t\0m"),
+	},
+},
 
 };
 
Signature · 1 line
2.30.2
maintainer
#3 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add a dedup selftest with equivalent structure types

Quoted 6 lines · Yonghong Song

Without previous libbpf patch, the following error will occur:

   $ ./test_progs -t btf
   ...
   do_test_dedup:FAIL:check btf_dedup failed errno:-22#13/205 btf/dedup: btf_type_tag #5, struct:FAIL

And the previfous libbpf patch fixed the issue.

Fixed up the typo above while applying and also formatted the 1/2 a bit better. checkpatch usually has a lot of noise in its output, but it would catch things like typos when quickly running before submission, fwiw. Anyway, thanks!

#4 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add a dedup selftest with equivalent structure types

Quoted 11 lines · Daniel Borkmann
Quoted 6 lines · Yonghong Song

Without previous libbpf patch, the following error will occur:

   $ ./test_progs -t btf
   ...
   do_test_dedup:FAIL:check btf_dedup failed errno:-22#13/205 btf/dedup: btf_type_tag #5, struct:FAIL

And the previfous libbpf patch fixed the issue.

Fixed up the typo above while applying and also formatted the 1/2 a bit better. checkpatch usually has a lot of noise in its output, but it would catch things like typos when quickly running before submission, fwiw. Anyway, thanks!

Thanks for the fixup! I need to remember to run checkpatch even for small patches!

#5 lore raw
In reply to Yonghong Song
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 9 lines · On Mon, 15 Nov 2021 08:39:32 -0800 you wrote:

Commit 2dc1e488e5cd ("libbpf: Support BTF_KIND_TYPE_TAG") added BTF_KIND_TYPE_TAG support. But BTF_KIND_TYPE_TAG is not handled properly in libbpf btf_dedup_is_equiv() which will cause pahole dedup failure if the kernel has the following hack:
#define __user __attribute__((btf_type_tag("user")))

Patch 1 fixed the issue and Patch 2 added a test for it.

[...]

Here is the summary with links:
- [bpf-next,1/2] libbpf: fix a couple of missed btf_type_tag handling in btf.c

    https://git.kernel.org/bpf/bpf-next/c/69a055d54615
  - [bpf-next,2/2] selftests/bpf: add a dedup selftest with equivalent structure types
    https://git.kernel.org/bpf/bpf-next/c/4746158305e9

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html