Skip to content

Commit

Permalink
observer_stats: use GetNumPossibleCPUs()
Browse files Browse the repository at this point in the history
runtime.NumCPU() returns the number of CPUs available to the process.
This excludes offline CPUs, but also does not take into acount task CPU
affinity.

For example:

```
$ cat numcpus.go
package main

import (
        "fmt"
        "runtime"
)

func main() {
        fmt.Printf("%d\n", runtime.NumCPU())
}
$ go run numcpus.go
16
$ taskset --cpu-list 0 go run numcpus.go
1
```

This leads to memory corruption because we pass to the bpf() call a
buffer smaller than the write it will perform, leading to data being
overwritten. Reducing the timeout of the goroutine that reads stat maps
will reproduce this quickly and lead to SIGSEGVs, corrupted prometheus
metrics or other symptoms.

Signed-off-by: Kornilios Kourtis <[email protected]>
  • Loading branch information
kkourt committed Jun 19, 2023
1 parent 590a524 commit 2df044e
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/observer/observer_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (k *statKey) String() string { return fmt.Sprintf("key=%d", k.Ke
func (k *statKey) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
func (k *statKey) NewValue() bpf.MapValue {
return &statValue{
Value: make([]int64, runtime.NumCPU()),
Value: make([]int64, bpf.GetNumPossibleCPUs()),
}
}
func (k *statKey) DeepCopyMapKey() bpf.MapKey { return &statKey{k.Key} }
Expand All @@ -41,7 +41,7 @@ func (s *statValue) GetValuePtr() unsafe.Pointer {
}
func (s *statValue) DeepCopyMapValue() bpf.MapValue {
v := &statValue{
Value: make([]int64, runtime.NumCPU()),
Value: make([]int64, len(s.Value)),
}
copy(v.Value, s.Value)
return v
Expand Down

0 comments on commit 2df044e

Please sign in to comment.