Skip to content

Commit

Permalink
program: don't return error when kmod BTF is disabled
Browse files Browse the repository at this point in the history
In kernels where the flag CONFIG_DEBUG_INFO_BTF_MODULES is not set,
including kernels 5.10 and below (because the flag was introduced in 5.11),
loading a program that attaches to kernel module functions and relied on
CORE failed because the module's BTF is not available.

This fix allows the program to run, obviously only as long as it only
relies on the kernel's BTF and not on the specific module's BTF.

Fixes: #1436
Signed-off-by: Ori Shussman <[email protected]>
  • Loading branch information
orishuss authored and lmb committed Apr 22, 2024
1 parent 7719d2f commit 20ddfe1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions btf/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func LoadKernelSpec() (*Spec, error) {
//
// Defaults to /sys/kernel/btf/<module>.
// Returns an error wrapping ErrNotSupported if BTF is not enabled.
// Returns an error wrapping fs.ErrNotExist if BTF for the specific module doesn't exist.
func LoadKernelModuleSpec(module string) (*Spec, error) {
kernelBTF.RLock()
spec := kernelBTF.modules[module]
Expand Down
8 changes: 6 additions & 2 deletions linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"math"
"slices"

Expand Down Expand Up @@ -148,10 +149,13 @@ func applyRelocations(insns asm.Instructions, targets []*btf.Spec, kmodName stri

if kmodName != "" {
kmodTarget, err := btf.LoadKernelModuleSpec(kmodName)
if err != nil {
// Ignore ErrNotExists to cater to kernels which have CONFIG_DEBUG_INFO_BTF_MODULES disabled.
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("load kernel module spec: %w", err)
}
targets = append(targets, kmodTarget)
if err == nil {
targets = append(targets, kmodTarget)
}
}
}

Expand Down

0 comments on commit 20ddfe1

Please sign in to comment.