Skip to content

Commit a785455

Browse files
paulcacheuxlmb
authored andcommitted
btf: fix race when loading cached kernel (module)? spec
When going from the fast read path to the slow write path, another thread may have already loaded the desired spec. This can cause loading the kernel spec twice, but also issues like rebasing a decoder from a base to another with the same content (both kernel spec) but different addresses. Signed-off-by: Paul Cacheux <[email protected]>
1 parent e82f639 commit a785455

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

btf/kernel.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ func loadCachedKernelSpec() (*Spec, error) {
6060
globalCache.Lock()
6161
defer globalCache.Unlock()
6262

63+
// check again, to prevent race between multiple callers
64+
if globalCache.kernel != nil {
65+
return globalCache.kernel, nil
66+
}
67+
6368
spec, err := loadKernelSpec()
6469
if err != nil {
6570
return nil, err
@@ -103,6 +108,11 @@ func loadCachedKernelModuleSpec(module string) (*Spec, error) {
103108
globalCache.Lock()
104109
defer globalCache.Unlock()
105110

111+
// check again, to prevent race between multiple callers
112+
if spec := globalCache.modules[module]; spec != nil {
113+
return spec, nil
114+
}
115+
106116
spec, err = loadKernelModuleSpec(module, base)
107117
if err != nil {
108118
return nil, err

0 commit comments

Comments
 (0)