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

[v1.2 backport] Memory optimizations: remove BTF and kallsyms caches #3036

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 1 addition & 11 deletions pkg/ksyms/ksyms.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,15 @@ import (
"sort"
"strconv"
"strings"
"sync"

"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/option"

lru "github.com/hashicorp/golang-lru/v2"
)

var (
kernelSymbols *Ksyms
setKernelSymbols sync.Once
)

func KernelSymbols() (*Ksyms, error) {
var err error
setKernelSymbols.Do(func() {
kernelSymbols, err = NewKsyms(option.Config.ProcFS)
})
return kernelSymbols, err
return NewKsyms(option.Config.ProcFS)
}

type ksym struct {
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 @@ -115,6 +116,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 @@ -913,6 +913,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
Loading