Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup generated BTF for older kernels #2934

Merged
merged 1 commit into from
Jan 22, 2024

Conversation

viktormalik
Copy link
Contributor

@viktormalik viktormalik commented Jan 18, 2024

Older kernels missing torvalds/linux@51c39bb1d5d1 (most importantly the stable 5.4) do not support BTF function entries with global linkage (BTF_FUNC_GLOBAL). This causes them to reject our generated BTF for probes and prevents from running any bpftrace program.

In order to fix this, the generated BTF must be patched by replacing the BTF_FUNC_GLOBAL flag by 0. This is normally done by libbpf (torvalds/linux@2d3eb67f64ec) but only when program loading via bpf_object is used. Since we're still using direct loading using bpf_btf_load and bpf_prog_load, we need to mimic libbpf's behavior and do the patching manually. Note that we cannot fix this directly in codegen b/c LLVM would optimize all our probes away.

The BTF fixup itself is pretty simple but required to copy a portion of code from libbpf internals. The reason is that libbpf doesn't export functions for iterating BTF in a non-const way.

This also requires to feature-detect support for BTF_FUNC_GLOBAL, so we extend BPFfeature with BTF feature detection.

Fixes #2913.

Checklist
  • Language changes are updated in man/adoc/bpftrace.adoc and if needed in docs/reference_guide.md
  • User-visible and non-trivial changes updated in CHANGELOG.md
  • The new behaviour is covered by tests

src/bpffeature.cpp Dismissed Show dismissed Hide dismissed
@viktormalik
Copy link
Contributor Author

@xiaoqing-wei @the-prachi do you think you'd be able to test this fix on your setup? Unfortunately, we don't build static binaries for PRs but you could build it yourself by running nix build .#appimage with this PR checked out.

I was able to test this on my local reproducer of #2913 but would like to have a confirmation that it fixes the issue.

CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Contributor

@jordalgo jordalgo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good but someone with more btf experience should have a look.

Older kernels missing [1] (most importantly the stable 5.4) do not
support BTF function entries with global linkage (BTF_FUNC_GLOBAL). This
causes them to reject our generated BTF for probes and prevents from
running any bpftrace program.

In order to fix this, the generated BTF must be patched by replacing the
BTF_FUNC_GLOBAL flag by 0. This is normally done by libbpf [2] but only
when program loading via bpf_object is used. Since we're still using
direct loading using bpf_btf_load and bpf_prog_load, we need to mimic
libbpf's behavior and do the patching manually. Note that we cannot fix
this directly in codegen b/c LLVM would optimize all our probes away.

The BTF fixup itself is pretty simple but required to copy a portion of
code from libbpf internals. The reason is that libbpf doesn't export
functions for iterating BTF in a non-const way.

This also requires to feature-detect support for BTF_FUNC_GLOBAL, so we
extend BPFfeature with BTF feature detection.

[1] torvalds/linux@51c39bb1d5d1
[2] torvalds/linux@2d3eb67f64ec
Copy link
Member

@ajor ajor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@viktormalik
Copy link
Contributor Author

I was able to reproduce the issue on 5.4.266 kernel and confirm that this fix works. So, I'll go ahead and merge this as it's blocking me on other work.

@viktormalik viktormalik merged commit 322cdfa into bpftrace:master Jan 22, 2024
19 checks passed
@viktormalik viktormalik deleted the btf-func-global branch January 22, 2024 07:21
@xiaoqing-wei
Copy link

@xiaoqing-wei @the-prachi do you think you'd be able to test this fix on your setup? Unfortunately, we don't build static binaries for PRs but you could build it yourself by running nix build .#appimage with this PR checked out.

I was able to test this on my local reproducer of #2913 but would like to have a confirmation that it fixes the issue.

I saw there's a new tagged build and it's working for me.

https://github.com/iovisor/bpftrace/releases/download/v0.20.0/binary_tools_man-bundle.tar.xz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

type_id=3 vlen != 0 on 5.4 LTS kernel
4 participants