Skip to content

Commit 159fd10

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 e0122ec commit 159fd10

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

pkg/sensors/tracing/generictracepoint.go

Lines changed: 37 additions & 38 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,15 +66,14 @@ 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

7674
Spec *v1alpha1.TracepointSpec
7775
policyID policyfilter.PolicyID
7876

79-
// index to access this on genericTracepointTable
80-
tableIdx int
81-
8277
// for tracepoints that have a GetUrl or DnsLookup action, we store the table of arguments.
8378
actionArgs idtable.Table
8479

@@ -103,6 +98,10 @@ type genericTracepoint struct {
10398
raw bool
10499
}
105100

101+
func (tp *genericTracepoint) SetID(id idtable.EntryID) {
102+
tp.tableId = id
103+
}
104+
106105
// genericTracepointArg is the internal representation of an output value of a
107106
// generic tracepoint.
108107
type genericTracepointArg struct {
@@ -138,30 +137,16 @@ type genericTracepointArg struct {
138137
btf [tracingapi.MaxBTFArgDepth]tracingapi.ConfigBTFArg
139138
}
140139

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
140+
func genericTracepointTableGet(id idtable.EntryID) (*genericTracepoint, error) {
141+
entry, err := genericTracepointTable.GetEntry(id)
142+
if err != nil {
143+
return nil, fmt.Errorf("getting entry from genericTracepointTable failed with: %w", err)
163144
}
164-
return nil, fmt.Errorf("tracepoint table: invalid id:%d (len=%d)", idx, len(t.arr))
145+
val, ok := entry.(*genericTracepoint)
146+
if !ok {
147+
return nil, fmt.Errorf("getting entry from genericTracepointTable failed with: got invalid type: %T (%v)", entry, entry)
148+
}
149+
return val, nil
165150
}
166151

167152
func (out *genericTracepointArg) String() string {
@@ -408,6 +393,7 @@ func createGenericTracepoint(
408393
}
409394

410395
ret := &genericTracepoint{
396+
tableId: idtable.UninitializedEntryID,
411397
Info: &tp,
412398
Spec: conf,
413399
args: tpArgs,
@@ -419,8 +405,8 @@ func createGenericTracepoint(
419405
raw: conf.Raw,
420406
}
421407

422-
genericTracepointTable.addTracepoint(ret)
423-
ret.pinPathPrefix = sensors.PathJoin(sensorName, fmt.Sprintf("gtp-%d", ret.tableIdx))
408+
genericTracepointTable.AddEntry(ret)
409+
ret.pinPathPrefix = sensors.PathJoin(sensorName, fmt.Sprintf("gtp-%d", ret.tableId.ID))
424410
return ret, nil
425411
}
426412

@@ -563,7 +549,7 @@ func createGenericTracepointSensor(
563549

564550
has.fdInstall = selectorsHaveFDInstall(tp.Spec.Selectors)
565551

566-
prog0.LoaderData = tp.tableIdx
552+
prog0.LoaderData = tp.tableId
567553
progs = append(progs, prog0)
568554

569555
fdinstall := program.MapBuilderSensor("fdinstall_map", prog0)
@@ -664,6 +650,19 @@ func createGenericTracepointSensor(
664650

665651
ret.Progs = progs
666652
ret.Maps = maps
653+
654+
ret.DestroyHook = func() error {
655+
var errs error
656+
657+
for _, tp := range tracepoints {
658+
_, err := genericTracepointTable.RemoveEntry(tp.tableId)
659+
if err != nil {
660+
errs = errors.Join(errs, err)
661+
}
662+
}
663+
return errs
664+
}
665+
667666
return ret, nil
668667
}
669668

@@ -719,7 +718,7 @@ func (tp *genericTracepoint) EventConfig() (*tracingapi.EventConfig, error) {
719718

720719
config := tracingapi.EventConfig{}
721720
config.PolicyID = uint32(tp.policyID)
722-
config.FuncId = uint32(tp.tableIdx)
721+
config.FuncId = uint32(tp.tableId.ID)
723722

724723
if tp.raw {
725724
return tp.eventConfigRaw(&config)
@@ -769,12 +768,12 @@ func LoadGenericTracepointSensor(bpfDir string, load *program.Program, maps []*p
769768

770769
tracepointLog = logger.GetLogger()
771770

772-
tpIdx, ok := load.LoaderData.(int)
771+
id, ok := load.LoaderData.(idtable.EntryID)
773772
if !ok {
774773
return fmt.Errorf("loaderData for genericTracepoint %s is %T (%v) (not an int)", load.Name, load.LoaderData, load.LoaderData)
775774
}
776775

777-
tp, err := genericTracepointTable.getTracepoint(tpIdx)
776+
tp, err := genericTracepointTableGet(id)
778777
if err != nil {
779778
return fmt.Errorf("could not find generic tracepoint information for %s: %w", load.Attach, err)
780779
}
@@ -820,7 +819,7 @@ func handleGenericTracepoint(r *bytes.Reader) ([]observer.Event, error) {
820819
Event: "UNKNOWN",
821820
}
822821

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

0 commit comments

Comments
 (0)