From 15794360e79470cc249c60d22628196295501d25 Mon Sep 17 00:00:00 2001 From: Guy Arbitman Date: Wed, 4 Dec 2024 13:36:39 +0200 Subject: [PATCH] sharedlibraries: Replace atomic.Int32 with regular uint16 No need to use atomic operations, as we're protected with a mutex --- pkg/network/usm/sharedlibraries/ebpf.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/network/usm/sharedlibraries/ebpf.go b/pkg/network/usm/sharedlibraries/ebpf.go index ce641f5b06f8a..1bbc352eb281c 100644 --- a/pkg/network/usm/sharedlibraries/ebpf.go +++ b/pkg/network/usm/sharedlibraries/ebpf.go @@ -16,8 +16,6 @@ import ( "strings" "sync" - "go.uber.org/atomic" - manager "github.com/DataDog/ebpf-manager" "golang.org/x/sys/unix" @@ -95,7 +93,7 @@ type EbpfProgram struct { // refcount is the number of times the program has been initialized. It is used to // stop the program only when the refcount reaches 0. - refcount atomic.Int32 + refcount uint16 // initMutex is a mutex to protect the initialization variables and the libset map wg sync.WaitGroup @@ -147,7 +145,7 @@ func GetEBPFProgram(cfg *ddebpf.Config) *EbpfProgram { } } - progSingleton.refcount.Inc() + progSingleton.refcount++ return progSingleton } @@ -447,10 +445,13 @@ func (e *EbpfProgram) Stop() { singletonMutex.Lock() defer singletonMutex.Unlock() - if e.refcount.Dec() != 0 { - if e.refcount.Load() < 0 { - e.refcount.Swap(0) - } + if e.refcount == 0 { + log.Warn("shared libraries monitor stopping with a refcount of 0") + return + } + e.refcount-- + if e.refcount > 0 { + // Still in use return }