Skip to content

Commit

Permalink
[btf] Flush kernel spec (BTF) after loading a sensor
Browse files Browse the repository at this point in the history
This patch flushes the BTF when we complete the loading of a sensor.

Signed-off-by: Anastasios Papagiannis <[email protected]>
  • Loading branch information
tpapagian committed Sep 20, 2024
1 parent 8ccba6c commit b04a57f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/bpf/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ func HasMissedStatsKprobeMulti() bool {
}

func LogFeatures() string {
// once we have detected all features, flush the BTF spec
// we cache all values so calling again a Has* function will
// not load the BTF again
defer ebtf.FlushKernelSpec()
return fmt.Sprintf("override_return: %t, buildid: %t, kprobe_multi: %t, uprobe_multi %t, fmodret: %t, fmodret_syscall: %t, signal: %t, large: %t, link_pin: %t, lsm: %t, missed_stats_kprobe_multi: %t, missed_stats_kprobe: %t",
HasOverrideHelper(), HasBuildId(), HasKprobeMulti(), HasUprobeMulti(),
HasModifyReturn(), HasModifyReturnSyscall(), HasSignalHelper(), HasProgramLargeSize(),
Expand Down
4 changes: 4 additions & 0 deletions pkg/sensors/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
cachedbtf "github.com/cilium/tetragon/pkg/btf"
"github.com/cilium/tetragon/pkg/kernels"
"github.com/cilium/tetragon/pkg/logger"
Expand Down Expand Up @@ -124,6 +125,9 @@ func (s *Sensor) Load(bpfDir string) error {
progsAdd(s.Progs)
AllMaps = append(AllMaps, s.Maps...)

// cleanup the BTF once we have loaded all sensor's program
btf.FlushKernelSpec()

l.WithField("sensor", s.Name).Infof("Loaded BPF maps and events for sensor successfully")
s.Loaded = true
return nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/sensors/program/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,11 @@ func doLoadProgram(

load.Prog = prog

// in KernelTypes, we use a non-standard BTF which is possibly annotated with symbols
// from kernel modules. At this point we don't need that anymore, so we can release
// the memory from it.
load.KernelTypes = nil

// Copy the loaded collection before it's destroyed
if KeepCollection {
return copyLoadedCollection(coll)
Expand Down

0 comments on commit b04a57f

Please sign in to comment.