diff --git a/map.go b/map.go index f3f98b46d..e5d8bd780 100644 --- a/map.go +++ b/map.go @@ -158,6 +158,17 @@ func (spec *MapSpec) fixupMagicFields() (*MapSpec, error) { // behaviour in the past. spec.MaxEntries = n } + + case CPUMap: + n, err := PossibleCPU() + if err != nil { + return nil, fmt.Errorf("fixup cpu map: %w", err) + } + + if n := uint32(n); spec.MaxEntries == 0 || spec.MaxEntries > n { + // Perform clamping similar to PerfEventArray. + spec.MaxEntries = n + } } return spec, nil diff --git a/map_test.go b/map_test.go index 9a65be1fa..a437a2648 100644 --- a/map_test.go +++ b/map_test.go @@ -981,6 +981,15 @@ func TestPerfEventArray(t *testing.T) { } } +func TestCPUMap(t *testing.T) { + testutils.SkipOnOldKernel(t, "4.15", "cpu map") + + m, err := NewMap(&MapSpec{Type: CPUMap, KeySize: 4, ValueSize: 4}) + qt.Assert(t, qt.IsNil(err)) + qt.Assert(t, qt.Equals(m.MaxEntries(), uint32(MustPossibleCPU()))) + m.Close() +} + func createMapInMap(t *testing.T, typ MapType) *Map { t.Helper()