Skip to content

Commit 570e2e6

Browse files
committed
Use Go 1.19 atomic types
The Go standard library provides types for atomic access since Go 1.19, see https://go.dev/doc/go1.19#atomic_types and https://pkg.go.dev/sync/atomic#pkg-types. These make working with atomically accessed variables type-safe and more ergonomic. This change also allows to get rid of the go.uber.org/atomic dependency. Signed-off-by: Tobias Klauser <[email protected]>
1 parent f3fb0ea commit 570e2e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+69
-2172
lines changed

.golangci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ linters:
1111
- errname
1212
- errorlint
1313
- exptostd
14-
- gomodguard
1514
- goheader
15+
- gomodguard
1616
- govet
1717
- ineffassign
1818
- intrange
@@ -55,6 +55,10 @@ linters:
5555
recommendations:
5656
- github.com/cilium/tetragon
5757
reason: cilium/cilium package might be overkilled, consider other alternatives
58+
- go.uber.org/atomic
59+
recommendations:
60+
- sync/atomic
61+
reason: Go 1.19+ supports atomic types, see https://go.dev/doc/go1.19#atomic_types
5862
exclusions:
5963
generated: lax
6064
presets:

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ require (
3838
github.com/stretchr/testify v1.10.0
3939
github.com/tidwall/gjson v1.18.0
4040
github.com/vishvananda/netlink v1.3.1
41-
go.uber.org/atomic v1.11.0
4241
go.uber.org/multierr v1.11.0
4342
golang.org/x/sync v0.15.0
4443
golang.org/x/sys v0.33.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,6 @@ go.opentelemetry.io/otel/trace v1.35.0/go.mod h1:WUk7DtFp1Aw2MkvqGdwiXYDZZNvA/1J
386386
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
387387
go.opentelemetry.io/proto/otlp v1.5.0 h1:xJvq7gMzB31/d406fB8U5CBdyQGw4P399D1aQWU/3i4=
388388
go.opentelemetry.io/proto/otlp v1.5.0/go.mod h1:keN8WnHxOy8PG0rQZjJJ5A2ebUoafqWp0eVQ4yIXvJ4=
389-
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
390-
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
391389
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
392390
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
393391
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=

pkg/bench/bench.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ func (bl *benchmarkListener) Close() error {
178178
}
179179

180180
type timingEncoder struct {
181-
totalDuration uint64
181+
totalDuration atomic.Uint64
182182
inner exporter.ExportEncoder
183183
}
184184

185185
func (te *timingEncoder) Encode(v interface{}) error {
186186
t0 := time.Now()
187187
err := te.inner.Encode(v)
188-
atomic.AddUint64(&te.totalDuration, uint64(time.Since(t0)))
188+
te.totalDuration.Add(uint64(time.Since(t0)))
189189
return err
190190
}
191191

@@ -248,7 +248,7 @@ func startBenchmarkExporter(ctx context.Context, obs *observer.Observer, summary
248248
// FIXME I'm racy, someone might read summary before this is written.
249249
// Likely not an issue since we wait for slower things to exit.
250250
<-ctx.Done()
251-
summary.JSONEncodingDurationNanos = time.Duration(timingEncoder.totalDuration)
251+
summary.JSONEncodingDurationNanos = time.Duration(timingEncoder.totalDuration.Load())
252252
}()
253253

254254
req := tetragon.GetEventsRequest{AllowList: nil, DenyList: nil, AggregationOptions: nil}

pkg/grpc/exec/exec_test_helper.go

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const (
3030

3131
var (
3232
AllEvents []*tetragon.GetEventsResponse
33-
BasePid uint32 = 46987
34-
dummyPod = &corev1.Pod{
33+
BasePid atomic.Uint32
34+
dummyPod = &corev1.Pod{
3535
ObjectMeta: v1.ObjectMeta{
3636
Namespace: "fake_pod_namespace",
3737
Name: "fake_pod_name",
@@ -56,6 +56,10 @@ var (
5656
}
5757
)
5858

59+
func init() {
60+
BasePid.Store(46987)
61+
}
62+
5963
type DummyNotifier[EXEC notify.Message, EXIT notify.Message] struct {
6064
t *testing.T
6165
ch chan bool
@@ -489,8 +493,8 @@ func GrpcExecOutOfOrder[EXEC notify.Message, EXIT notify.Message](t *testing.T)
489493
watcher := watcher.NewFakeK8sWatcher(nil)
490494
dn := InitEnv[EXEC, EXIT](t, watcher)
491495

492-
parentPid := atomic.AddUint32(&BasePid, 1)
493-
currentPid := atomic.AddUint32(&BasePid, 1)
496+
parentPid := BasePid.Add(1)
497+
currentPid := BasePid.Add(1)
494498

495499
execRoot, execParent, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "")
496500

@@ -519,8 +523,8 @@ func GrpcExecInOrder[EXEC notify.Message, EXIT notify.Message](t *testing.T) {
519523
watcher := watcher.NewFakeK8sWatcher(nil)
520524
InitEnv[EXEC, EXIT](t, watcher)
521525

522-
parentPid := atomic.AddUint32(&BasePid, 1)
523-
currentPid := atomic.AddUint32(&BasePid, 1)
526+
parentPid := BasePid.Add(1)
527+
currentPid := BasePid.Add(1)
524528

525529
execRoot, execParent, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "")
526530

@@ -548,8 +552,8 @@ func GrpcExecMisingParent[EXEC notify.Message, EXIT notify.Message](t *testing.T
548552
watcher := watcher.NewFakeK8sWatcher(nil)
549553
dn := InitEnv[EXEC, EXIT](t, watcher)
550554

551-
parentPid := atomic.AddUint32(&BasePid, 1)
552-
currentPid := atomic.AddUint32(&BasePid, 1)
555+
parentPid := BasePid.Add(1)
556+
currentPid := BasePid.Add(1)
553557

554558
_, _, execMsg, _ := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "")
555559

@@ -573,8 +577,8 @@ func GrpcMissingExec[EXEC notify.Message, EXIT notify.Message](t *testing.T) {
573577
watcher := watcher.NewFakeK8sWatcher(nil)
574578
dn := InitEnv[EXEC, EXIT](t, watcher)
575579

576-
parentPid := atomic.AddUint32(&BasePid, 1)
577-
currentPid := atomic.AddUint32(&BasePid, 1)
580+
parentPid := BasePid.Add(1)
581+
currentPid := BasePid.Add(1)
578582

579583
_, _, _, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "")
580584

@@ -601,8 +605,8 @@ func GrpcExecParentOutOfOrder[EXEC notify.Message, EXIT notify.Message](t *testi
601605
watcher := watcher.NewFakeK8sWatcher(nil)
602606
InitEnv[EXEC, EXIT](t, watcher)
603607

604-
parentPid := atomic.AddUint32(&BasePid, 1)
605-
currentPid := atomic.AddUint32(&BasePid, 1)
608+
parentPid := BasePid.Add(1)
609+
currentPid := BasePid.Add(1)
606610

607611
execRoot, execParent, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "")
608612

@@ -663,9 +667,9 @@ func GrpcExecCloneInOrder[EXEC notify.Message, CLONE notify.Message, EXIT notify
663667
watcher := watcher.NewFakeK8sWatcher(nil)
664668
InitEnv[EXEC, EXIT](t, watcher)
665669

666-
parentPid := atomic.AddUint32(&BasePid, 1)
667-
currentPid := atomic.AddUint32(&BasePid, 1)
668-
clonePid := atomic.AddUint32(&BasePid, 1)
670+
parentPid := BasePid.Add(1)
671+
currentPid := BasePid.Add(1)
672+
clonePid := BasePid.Add(1)
669673

670674
execRoot, execParent, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "")
671675
cloneMsg, exitCloneMsg := CreateCloneEvents[CLONE, EXIT](clonePid, 21034995089403, currentPid, 21034975089403)
@@ -696,9 +700,9 @@ func GrpcExecCloneOutOfOrder[EXEC notify.Message, CLONE notify.Message, EXIT not
696700
watcher := watcher.NewFakeK8sWatcher(nil)
697701
dn := InitEnv[EXEC, EXIT](t, watcher)
698702

699-
parentPid := atomic.AddUint32(&BasePid, 1)
700-
currentPid := atomic.AddUint32(&BasePid, 1)
701-
clonePid := atomic.AddUint32(&BasePid, 1)
703+
parentPid := BasePid.Add(1)
704+
currentPid := BasePid.Add(1)
705+
clonePid := BasePid.Add(1)
702706

703707
execRoot, execParent, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "")
704708
cloneMsg, exitCloneMsg := CreateCloneEvents[CLONE, EXIT](clonePid, 21034995089403, currentPid, 21034975089403)
@@ -731,8 +735,8 @@ func GrpcParentInOrder[EXEC notify.Message, EXIT notify.Message](t *testing.T) {
731735
watcher := watcher.NewFakeK8sWatcher(nil)
732736
InitEnv[EXEC, EXIT](t, watcher)
733737

734-
parentPid := atomic.AddUint32(&BasePid, 1)
735-
currentPid := atomic.AddUint32(&BasePid, 1)
738+
parentPid := BasePid.Add(1)
739+
currentPid := BasePid.Add(1)
736740

737741
rootMsg, _, parentExecMsg, parentExitMsg := CreateEvents[EXEC, EXIT](parentPid, 75200000000, 1, 0, "")
738742
_, _, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "")
@@ -822,8 +826,8 @@ func GrpcExecPodInfoInOrder[EXEC notify.Message, EXIT notify.Message](t *testing
822826
fakeWatcher := watcher.NewFakeK8sWatcher(nil)
823827
dn := InitEnv[EXEC, EXIT](t, fakeWatcher)
824828

825-
parentPid := atomic.AddUint32(&BasePid, 1)
826-
currentPid := atomic.AddUint32(&BasePid, 1)
829+
parentPid := BasePid.Add(1)
830+
currentPid := BasePid.Add(1)
827831

828832
rootMsg, parentMsg, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "fake_container_container_id")
829833

@@ -860,8 +864,8 @@ func GrpcExecPodInfoOutOfOrder[EXEC notify.Message, EXIT notify.Message](t *test
860864
fakeWatcher := watcher.NewFakeK8sWatcher(nil)
861865
dn := InitEnv[EXEC, EXIT](t, fakeWatcher)
862866

863-
parentPid := atomic.AddUint32(&BasePid, 1)
864-
currentPid := atomic.AddUint32(&BasePid, 1)
867+
parentPid := BasePid.Add(1)
868+
currentPid := BasePid.Add(1)
865869

866870
rootMsg, parentMsg, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "fake_container_container_id")
867871

@@ -901,8 +905,8 @@ func GrpcExecPodInfoInOrderAfter[EXEC notify.Message, EXIT notify.Message](t *te
901905
fakeWatcher := watcher.NewFakeK8sWatcher(nil)
902906
dn := InitEnv[EXEC, EXIT](t, fakeWatcher)
903907

904-
parentPid := atomic.AddUint32(&BasePid, 1)
905-
currentPid := atomic.AddUint32(&BasePid, 1)
908+
parentPid := BasePid.Add(1)
909+
currentPid := BasePid.Add(1)
906910

907911
rootMsg, parentMsg, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "fake_container_container_id")
908912

@@ -943,8 +947,8 @@ func GrpcExecPodInfoOutOfOrderAfter[EXEC notify.Message, EXIT notify.Message](t
943947
fakeWatcher := watcher.NewFakeK8sWatcher(nil)
944948
dn := InitEnv[EXEC, EXIT](t, fakeWatcher)
945949

946-
parentPid := atomic.AddUint32(&BasePid, 1)
947-
currentPid := atomic.AddUint32(&BasePid, 1)
950+
parentPid := BasePid.Add(1)
951+
currentPid := BasePid.Add(1)
948952

949953
rootMsg, parentMsg, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "fake_container_container_id")
950954

@@ -982,8 +986,8 @@ func GrpcExecPodInfoDelayedOutOfOrder[EXEC notify.Message, EXIT notify.Message](
982986
fakeWatcher := watcher.NewFakeK8sWatcher(nil)
983987
dn := InitEnv[EXEC, EXIT](t, fakeWatcher)
984988

985-
parentPid := atomic.AddUint32(&BasePid, 1)
986-
currentPid := atomic.AddUint32(&BasePid, 1)
989+
parentPid := BasePid.Add(1)
990+
currentPid := BasePid.Add(1)
987991

988992
rootMsg, parentMsg, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "fake_container_container_id")
989993

@@ -1027,8 +1031,8 @@ func GrpcExecPodInfoDelayedInOrder[EXEC notify.Message, EXIT notify.Message](t *
10271031
fakeWatcher := watcher.NewFakeK8sWatcher(nil)
10281032
dn := InitEnv[EXEC, EXIT](t, fakeWatcher)
10291033

1030-
parentPid := atomic.AddUint32(&BasePid, 1)
1031-
currentPid := atomic.AddUint32(&BasePid, 1)
1034+
parentPid := BasePid.Add(1)
1035+
currentPid := BasePid.Add(1)
10321036

10331037
rootMsg, parentMsg, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "fake_container_container_id")
10341038

@@ -1071,8 +1075,8 @@ func GrpcDelayedExecK8sOutOfOrder[EXEC notify.Message, EXIT notify.Message](t *t
10711075
fakeWatcher := watcher.NewFakeK8sWatcher(nil)
10721076
dn := InitEnv[EXEC, EXIT](t, fakeWatcher)
10731077

1074-
parentPid := atomic.AddUint32(&BasePid, 1)
1075-
currentPid := atomic.AddUint32(&BasePid, 1)
1078+
parentPid := BasePid.Add(1)
1079+
currentPid := BasePid.Add(1)
10761080

10771081
rootMsg, parentMsg, execMsg, exitMsg := CreateEvents[EXEC, EXIT](currentPid, 21034975089403, parentPid, 75200000000, "fake_container_container_id")
10781082

pkg/policyfilter/k8s_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"log/slog"
1313
"math/rand"
14+
"sync/atomic"
1415
"testing"
1516
"time"
1617

@@ -20,7 +21,6 @@ import (
2021
"github.com/cilium/tetragon/pkg/option"
2122
"github.com/google/uuid"
2223
"github.com/stretchr/testify/require"
23-
"go.uber.org/atomic"
2424
v1 "k8s.io/api/core/v1"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
k8stypes "k8s.io/apimachinery/pkg/types"

pkg/process/cache.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"maps"
99
"path/filepath"
10-
"sync/atomic"
1110
"time"
1211

1312
"github.com/cilium/ebpf"
@@ -77,8 +76,7 @@ func (pc *Cache) cacheGarbageCollector(intervalGC time.Duration) {
7776
* later if we care. Also we may try to delete the
7877
* process a second time, but that is harmless.
7978
*/
80-
ref := atomic.LoadUint32(&p.refcnt)
81-
if ref != 0 {
79+
if p.refcnt.Load() != 0 {
8280
continue
8381
}
8482
if p.color == deleteReady {
@@ -124,7 +122,7 @@ func (pc *Cache) refDec(p *ProcessInternal, reason string) {
124122
// count number of times refcnt is decremented for a specific reason (i.e. process, parent, etc.)
125123
p.refcntOps[reason]++
126124
p.refcntOpsLock.Unlock()
127-
ref := atomic.AddUint32(&p.refcnt, ^uint32(0))
125+
ref := p.refcnt.Add(^uint32(0))
128126
if ref == 0 {
129127
pc.deletePending(p)
130128
}
@@ -135,7 +133,7 @@ func (pc *Cache) refInc(p *ProcessInternal, reason string) {
135133
// count number of times refcnt is increamented for a specific reason (i.e. process, parent, etc.)
136134
p.refcntOps[reason]++
137135
p.refcntOpsLock.Unlock()
138-
atomic.AddUint32(&p.refcnt, 1)
136+
p.refcnt.Add(1)
139137
}
140138

141139
func (pc *Cache) purge() {
@@ -213,7 +211,8 @@ func (pc *Cache) dump(opts *tetragon.DumpProcessCacheReqArgs) []*tetragon.Proces
213211

214212
var processes []*tetragon.ProcessInternal
215213
for _, v := range pc.cache.Values() {
216-
if opts.SkipZeroRefcnt && v.refcnt == 0 {
214+
ref := v.refcnt.Load()
215+
if opts.SkipZeroRefcnt && ref == 0 {
217216
continue
218217
}
219218
if opts.ExcludeExecveMapProcesses {
@@ -225,7 +224,7 @@ func (pc *Cache) dump(opts *tetragon.DumpProcessCacheReqArgs) []*tetragon.Proces
225224
}
226225
processes = append(processes, &tetragon.ProcessInternal{
227226
Process: proto.Clone(v.process).(*tetragon.Process),
228-
Refcnt: &wrapperspb.UInt32Value{Value: v.refcnt},
227+
Refcnt: &wrapperspb.UInt32Value{Value: ref},
229228
RefcntOps: maps.Clone(v.refcntOps),
230229
Color: colorStr[v.color],
231230
})
@@ -238,7 +237,7 @@ func (pc *Cache) getEntries() []*tetragon.ProcessInternal {
238237
for _, v := range pc.cache.Values() {
239238
processes = append(processes, &tetragon.ProcessInternal{
240239
Process: v.process,
241-
Refcnt: &wrapperspb.UInt32Value{Value: v.refcnt},
240+
Refcnt: &wrapperspb.UInt32Value{Value: v.refcnt.Load()},
242241
RefcntOps: v.refcntOps,
243242
Color: colorStr[v.color],
244243
})

pkg/process/process.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type ProcessInternal struct {
4848
apiBinaryProp *tetragon.BinaryProperties
4949
// garbage collector metadata
5050
color int // Writes should happen only inside gc select channel
51-
refcnt uint32
51+
refcnt atomic.Uint32
5252
// refcntOps is a map of operations to refcnt change
5353
// keys can be:
5454
// - "process++": process increased refcnt (i.e. this process starts)
@@ -97,7 +97,7 @@ func (pi *ProcessInternal) GetProcessCopy() *tetragon.Process {
9797
pi.mu.Lock()
9898
proc := proto.Clone(pi.process).(*tetragon.Process)
9999
pi.mu.Unlock()
100-
proc.Refcnt = atomic.LoadUint32(&pi.refcnt)
100+
proc.Refcnt = pi.refcnt.Load()
101101
return proc
102102
}
103103

@@ -106,15 +106,16 @@ func (pi *ProcessInternal) GetProcessCopy() *tetragon.Process {
106106
func (pi *ProcessInternal) cloneInternalProcessCopy() *ProcessInternal {
107107
pi.mu.Lock()
108108
defer pi.mu.Unlock()
109-
return &ProcessInternal{
109+
npi := &ProcessInternal{
110110
process: proto.Clone(pi.process).(*tetragon.Process),
111111
capabilities: pi.capabilities,
112112
apiCreds: pi.apiCreds,
113113
apiBinaryProp: pi.apiBinaryProp,
114114
namespaces: pi.namespaces,
115-
refcnt: 1, // Explicitly initialize refcnt to 1
116115
refcntOps: map[string]int32{"process++": 1},
117116
}
117+
npi.refcnt.Store(1) // Explicitly initialize refcnt to 1
118+
return npi
118119
}
119120

120121
func (pi *ProcessInternal) AddPodInfo(pod *tetragon.Pod) {
@@ -226,7 +227,7 @@ func (pi *ProcessInternal) RefInc(reason string) {
226227
}
227228

228229
func (pi *ProcessInternal) RefGet() uint32 {
229-
return atomic.LoadUint32(&pi.refcnt)
230+
return pi.refcnt.Load()
230231
}
231232

232233
func (pi *ProcessInternal) NeededAncestors() bool {
@@ -384,9 +385,9 @@ func initProcessInternalExec(
384385
apiCreds: apiCreds,
385386
apiBinaryProp: apiBinaryProp,
386387
namespaces: apiNs,
387-
refcnt: 1,
388388
refcntOps: map[string]int32{"process++": 1},
389389
}
390+
pi.refcnt.Store(1)
390391

391392
// Set in_init_tree flag
392393
if event.Process.Flags&api.EventInInitTree == api.EventInInitTree {

0 commit comments

Comments
 (0)