Skip to content

Commit 2f22dd8

Browse files
committed
tetragon: Release entries in tracepoint sensor
Properly release tracepoint entries when sensor is destroyed. Signed-off-by: Jiri Olsa <[email protected]>
1 parent ed5cc57 commit 2f22dd8

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

pkg/sensors/tracing/generictracepoint.go

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"errors"
1212
"fmt"
1313
"path"
14-
"sync"
1514

1615
"github.com/cilium/ebpf"
1716
"github.com/cilium/tetragon/pkg/api/ops"
@@ -48,10 +47,7 @@ const (
4847
)
4948

5049
var (
51-
// Tracepoint information (genericTracepoint) is needed at load time
52-
// and at the time we process the perf event from bpf-side. We keep
53-
// this information on a table index by a (unique) tracepoint id.
54-
genericTracepointTable = tracepointTable{}
50+
genericTracepointTable idtable.Table
5551

5652
tracepointLog logrus.FieldLogger
5753
)
@@ -70,6 +66,8 @@ func init() {
7066

7167
// genericTracepoint is the internal representation of a tracepoint
7268
type genericTracepoint struct {
69+
tableId idtable.EntryID
70+
7371
Info *tracepoint.Tracepoint
7472
args []genericTracepointArg
7573

@@ -103,6 +101,10 @@ type genericTracepoint struct {
103101
raw bool
104102
}
105103

104+
func (tp *genericTracepoint) SetID(id idtable.EntryID) {
105+
tp.tableId = id
106+
}
107+
106108
// genericTracepointArg is the internal representation of an output value of a
107109
// generic tracepoint.
108110
type genericTracepointArg struct {
@@ -138,30 +140,16 @@ type genericTracepointArg struct {
138140
btf [tracingapi.MaxBTFArgDepth]tracingapi.ConfigBTFArg
139141
}
140142

141-
// tracepointTable is, for now, an array.
142-
type tracepointTable struct {
143-
mu sync.Mutex
144-
arr []*genericTracepoint
145-
}
146-
147-
// addTracepoint adds a tracepoint to the table, and sets its .tableIdx field
148-
// to be the index to retrieve it from the table.
149-
func (t *tracepointTable) addTracepoint(tp *genericTracepoint) {
150-
t.mu.Lock()
151-
defer t.mu.Unlock()
152-
idx := len(t.arr)
153-
t.arr = append(t.arr, tp)
154-
tp.tableIdx = idx
155-
}
156-
157-
// getTracepoint retrieves a tracepoint from the table using its id
158-
func (t *tracepointTable) getTracepoint(idx int) (*genericTracepoint, error) {
159-
t.mu.Lock()
160-
defer t.mu.Unlock()
161-
if idx < len(t.arr) {
162-
return t.arr[idx], nil
143+
func genericTracepointTableGet(id idtable.EntryID) (*genericTracepoint, error) {
144+
entry, err := genericTracepointTable.GetEntry(id)
145+
if err != nil {
146+
return nil, fmt.Errorf("getting entry from genericTracepointTable failed with: %w", err)
163147
}
164-
return nil, fmt.Errorf("tracepoint table: invalid id:%d (len=%d)", idx, len(t.arr))
148+
val, ok := entry.(*genericTracepoint)
149+
if !ok {
150+
return nil, fmt.Errorf("getting entry from genericTracepointTable failed with: got invalid type: %T (%v)", entry, entry)
151+
}
152+
return val, nil
165153
}
166154

167155
func (out *genericTracepointArg) String() string {
@@ -408,6 +396,7 @@ func createGenericTracepoint(
408396
}
409397

410398
ret := &genericTracepoint{
399+
tableId: idtable.UninitializedEntryID,
411400
Info: &tp,
412401
Spec: conf,
413402
args: tpArgs,
@@ -419,7 +408,7 @@ func createGenericTracepoint(
419408
raw: conf.Raw,
420409
}
421410

422-
genericTracepointTable.addTracepoint(ret)
411+
genericTracepointTable.AddEntry(ret)
423412
ret.pinPathPrefix = sensors.PathJoin(sensorName, fmt.Sprintf("gtp-%d", ret.tableIdx))
424413
return ret, nil
425414
}
@@ -563,7 +552,7 @@ func createGenericTracepointSensor(
563552

564553
has.fdInstall = selectorsHaveFDInstall(tp.Spec.Selectors)
565554

566-
prog0.LoaderData = tp.tableIdx
555+
prog0.LoaderData = tp.tableId
567556
progs = append(progs, prog0)
568557

569558
fdinstall := program.MapBuilderSensor("fdinstall_map", prog0)
@@ -664,6 +653,19 @@ func createGenericTracepointSensor(
664653

665654
ret.Progs = progs
666655
ret.Maps = maps
656+
657+
ret.DestroyHook = func() error {
658+
var errs error
659+
660+
for _, tp := range tracepoints {
661+
_, err := genericTracepointTable.RemoveEntry(tp.tableId)
662+
if err != nil {
663+
errs = errors.Join(errs, err)
664+
}
665+
}
666+
return errs
667+
}
668+
667669
return ret, nil
668670
}
669671

@@ -769,12 +771,12 @@ func LoadGenericTracepointSensor(bpfDir string, load *program.Program, maps []*p
769771

770772
tracepointLog = logger.GetLogger()
771773

772-
tpIdx, ok := load.LoaderData.(int)
774+
id, ok := load.LoaderData.(idtable.EntryID)
773775
if !ok {
774776
return fmt.Errorf("loaderData for genericTracepoint %s is %T (%v) (not an int)", load.Name, load.LoaderData, load.LoaderData)
775777
}
776778

777-
tp, err := genericTracepointTable.getTracepoint(tpIdx)
779+
tp, err := genericTracepointTableGet(id)
778780
if err != nil {
779781
return fmt.Errorf("could not find generic tracepoint information for %s: %w", load.Attach, err)
780782
}
@@ -820,7 +822,7 @@ func handleGenericTracepoint(r *bytes.Reader) ([]observer.Event, error) {
820822
Event: "UNKNOWN",
821823
}
822824

823-
tp, err := genericTracepointTable.getTracepoint(int(m.FuncId))
825+
tp, err := genericTracepointTableGet(idtable.EntryID{ID: int(m.FuncId)})
824826
if err != nil {
825827
logger.GetLogger().WithField("id", m.FuncId).WithError(err).Warnf("genericTracepoint info not found")
826828
return []observer.Event{unix}, nil

0 commit comments

Comments
 (0)