bpf: fix a couple of missed btf_type_tag handling in libbpf
Subsystems (4)
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
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.
--- tools/lib/bpf/btf.c | 2 ++ 1 file changed, 2 insertions(+)
tools/lib/bpf/btf.c +2 -0
@@ -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
PATCH 2/2 selftests/bpf: add a dedup selftest with equivalent structure types
In reply to Yonghong SongWithout 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
@@ -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
Daniel Borkmann
maintainerRe: [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:FAILAnd 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!
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:FAILAnd 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!
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: SupportBTF_KIND_TYPE_TAG") addedBTF_KIND_TYPE_TAGsupport. ButBTF_KIND_TYPE_TAGis not handled properly in libbpfbtf_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